-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0054 螺旋矩阵.cpp
More file actions
27 lines (27 loc) · 784 Bytes
/
0054 螺旋矩阵.cpp
File metadata and controls
27 lines (27 loc) · 784 Bytes
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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty())
return {};
vector<int> res;
int U = 0;
int D = matrix.size()-1;
int L = 0;
int R = matrix[0].size()-1;
while (true) {
for (int j=L; j<=R; ++j)
res.push_back(matrix[U][j]);
if (++U > D) break;
for (int i=U; i<=D; ++i)
res.push_back(matrix[i][R]);
if (--R < L) break;
for (int j=R; j>=L; --j)
res.push_back(matrix[D][j]);
if (--D < U) break;
for (int i=D; i>=U; --i)
res.push_back(matrix[i][L]);
if (++L > R) break;
}
return res;
}
};