-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Reverse Spiral Form of Matrix
61 lines (59 loc) · 1.43 KB
/
GFG Reverse Spiral Form of Matrix
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
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public:
vector<int> reverseSpiral(int R, int C, vector<vector<int>>&a) {
vector<int> ans;
int row=R;
int col=C;
int k=0;
int r,c;
while(row>=0 && col>=0)
{
r=k,c=k;
while(c<col)
{
//cout<<"first"<<endl;
//cout<<a[r][c]<<endl;
ans.push_back(a[r][c]);
++c;
}
if(ans.size()==R*C)
break;
--c;
r++;
while(r<row)
{
//cout<<"second"<<endl;
//cout<<a[r][c]<<endl;
ans.push_back(a[r][c]);
++r;
}
if(ans.size()==R*C)
break;
--c;
--r;
while(c>=k)
{
//cout<<"third"<<endl;
//cout<<a[r][c]<<endl;
ans.push_back(a[r][c]);
--c;
}
if(ans.size()==R*C)
break;
c++;
--r;
while(r>=k+1)
{
//cout<<"fourth"<<endl;
//cout<<a[r][c]<<endl;
ans.push_back(a[r][c]);
--r;
}
if(ans.size()==R*C)
break;
k++;--row;--col;
}
reverse(ans.begin(),ans.end());
return ans;
}
};