-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0054.cpp
More file actions
31 lines (31 loc) · 751 Bytes
/
0054.cpp
File metadata and controls
31 lines (31 loc) · 751 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
28
29
30
31
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &mat) {
int top = 0, bottom = mat.size() - 1;
int left = 0, right = mat[0].size() - 1;
vector<int> v;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) {
v.push_back(mat[top][i]);
}
top++;
for (int i = top; i <= bottom; i++) {
v.push_back(mat[i][right]);
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
v.push_back(mat[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
v.push_back(mat[i][left]);
}
left++;
}
}
return v;
}
};