Untitled
user_9124840
plain_text
a month ago
1.0 kB
2
Indexable
Never
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { int n = matrix.size(), m = matrix[0].size(); vector<int> array; int left = 0, right = m - 1; int top = 0, bottom = n - 1; while (left <= right && top <= bottom) { for (int i = left; i <= right; ++i) { array.push_back(matrix[top][i]); } top++; for (int i = top; i <= bottom; ++i) { array.push_back(matrix[i][right]); } right--; if (top <= bottom) { for (int i = right; i >= left; --i) { array.push_back(matrix[bottom][i]); } bottom--; } if (left <= right) { for (int i = bottom; i >= top; --i) { array.push_back(matrix[i][left]); } left++; } } return array; } };
Leave a Comment