-
Notifications
You must be signed in to change notification settings - Fork 0
/
54. Spiral Matrix.cpp
35 lines (28 loc) · 1.04 KB
/
54. Spiral Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
int minRow = 0, minCol = 0, maxRow = m - 1, maxCol = n - 1;
int i = 0, j = 0;
vector<int> seq;
while (minRow <= maxRow && minCol <= maxCol) {
// Traverse from left to right
while (j <= maxCol) seq.push_back(matrix[i][j++]);
minRow++; i++; j--;
// Traverse from top to bottom
while (i <= maxRow) seq.push_back(matrix[i++][j]);
maxCol--; j--; i--;
// Traverse from right to left (if still in bounds)
if (minRow <= maxRow) {
while (j >= minCol) seq.push_back(matrix[i][j--]);
maxRow--; i--; j++;
}
// Traverse from bottom to top (if still in bounds)
if (minCol <= maxCol) {
while (i >= minRow) seq.push_back(matrix[i--][j]);
minCol++; i++; j++;
}
}
return seq;
}
};