-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy path0054. Spiral Matrix.cpp
48 lines (45 loc) · 1.28 KB
/
0054. 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
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
int top = 0, down = n - 1, left = 0, right = m - 1;
int direction = 0;
vector<int> v;
while (top <= down && left <= right)
{
if (direction == 0) // left to right
{
for (int i = left; i <= right; ++i)
v.push_back(matrix[top][i]);
top += 1;
}
else if (direction == 1) // top to bottom
{
for (int i = top; i <= down; ++i)
v.push_back(matrix[i][right]);
right -= 1;
}
else if (direction == 2) // right to left
{
for (int i = right; i >= left; --i)
{
v.push_back(matrix[down][i]);
}
down -= 1;
}
else if (direction == 3) // down to top
{
for (int i = down; i >= top; --i)
{
v.push_back(matrix[i][left]);
}
left += 1;
}
direction = (direction + 1) % 4;
}
return v;
}
};