题解 | #螺旋矩阵#
螺旋矩阵
https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix int整型vector<vector<>> * @return int整型vector */ vector<int> spiralOrder(vector<vector<int> >& matrix) { // write code here std::vector<int> res; if (matrix.size() == 0) { return res; } int left = 0; int right = matrix[0].size() - 1; int up = 0; int down = matrix.size() - 1; while (true) { // 向右 if (left > right) { break; } for (int i = left; i <= right; i++) { res.push_back(matrix[left][i]); } up++; // 向下 if (up > down) { break; } for (int i = up; i <= down; i++) { res.push_back(matrix[i][right]); } right--; // 向左 if (left > right) { break; } for (int i = right; i >= left; i--) { res.push_back(matrix[down][i]); } down--; // 向上 if (up > down) { break; } for (int i = down; i >= up; i--) { res.push_back(matrix[i][left]); } left++; } return res; } };