-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek 2.q3
38 lines (38 loc) · 873 Bytes
/
Week 2.q3
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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int i,t,b,r,l;
t=0;
b=matrix[0].size()-1;
r=matrix.size()-1;
l=0;
while(t<=b &&l<=r)
{
for(i=t;i<=b;i++)
{
ans.push_back(matrix[t][i]);
}
for(i=l+1;i<=r;i++)
{
ans.push_back(matrix[i][r]);
}
if(t<b && l<r)
{
for(i=b-1;i>=t+1;i--)
{
ans.push_back(matrix[b][i]);
}
for(i=r;i>=l+1;i--)
{
ans.push_back(matrix[i][l]);
}
}
t++;
l++;
b--;
r--;
}
return ans;
}
};