-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprial.cpp
52 lines (50 loc) · 986 Bytes
/
sprial.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
49
50
51
52
vector<int> Solution::spiralOrder(const vector<vector<int> > &A) {
vector<int>s;
int t=0,i;
int b =A.size()-1;
int l = 0;
int r = A[0].size()-1;
int dir =0;
while(t<=b && l<=r)
{
if(dir==0)
{
for(i=l;i<=r;i++)
{
s.push_back(A[t][i]);
}
t++;
//dir =1;
}
if(dir==1)
{
for(i=t;i<=b;i++)
{
s.push_back(A[i][r]);
}
r--;
//dir =2;
}
if(dir==2)
{
for(i=r;i>=l;i--)
{
s.push_back(A[b][i]);
}
b--;
// dir =3;
}
if(dir==3)
{
for(i=b;i>=t;i--)
{
s.push_back(A[i][l]);
}
l++;
//dir =0;
}
dir = (dir + 1)%4;
}
// return Solution;
return(s);
}