-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0054.cpp
37 lines (31 loc) · 870 Bytes
/
0054.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
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int l = 0, r = matrix[0].size()-1, t = 0, b = matrix.size()-1;
vector<int> ans;
while (l <= r && t <= b)
{
for (int i = l; i <= r; i++)
ans.push_back(matrix[t][i]);
t++;
for (int j = t; j <= b; j++)
ans.push_back(matrix[j][r]);
r--;
if (t <= b)
{
for (int i = r; i >= l; i--)
ans.push_back(matrix[b][i]);
b--;
}
if (l <= r)
{
for (int j = b; j >= t; j--)
ans.push_back(matrix[j][l]);
l++;
}
}
return ans;
}
};