vector<int> Array::spiralOrder(vector<vector<int>>& matrix) { int m = matrix.size(); int n = matrix[0].size();
if (m == 0 || n == 0) return {};
vector<int> res; int left = 0, right = n - 1, top = 0, buttom = m - 1; while (left <= right && top <= buttom) { //在一次循环中将一次顺时针的四个方向遍历完成 //向右遍历的部分 for (int column = left; column <= right; column++) { res.push_back(matrix[top][column]); }
//向下遍历的部分 for (int row = top + 1; row <= buttom; row++) { res.push_back(matrix[row][right]); } //向左和向上遍历的部分 if (left < right&& top < buttom) { for (int column = right - 1; column > left; column--) { res.push_back(matrix[buttom][column]); }