-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54. Spiral Matrix.cpp
44 lines (43 loc) · 1022 Bytes
/
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
36
37
38
39
40
41
42
43
44
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>ans;
int n=matrix.size();
int m=matrix[0].size();
int i=0,j=m-1,k=n-1,l=0;
while(i<=k && l<=j)
{
if(i<=k)
{
for(int x=l;x<=j;x++)
{
ans.push_back(matrix[i][x]);
}
i++;
}
if(j>=l)
{
for(int z=i;z<=k;z++)
ans.push_back(matrix[z][j]);
j--;
}
if(k>=i)
{
for(int x=j;x>=l;x--)
{
ans.push_back(matrix[k][x]);
}
k--;
}
if(l<=j)
{
for(int x=k;x>=i;x--)
{
ans.push_back(matrix[x][l]);
}
l++;
}
}
return ans;
}
};