-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Order Matrix I.cpp
62 lines (53 loc) · 1.51 KB
/
Spiral Order Matrix I.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
53
54
55
56
57
58
59
60
61
vector<int> Solution::spiralOrder(const vector<vector<int> > &arr) {
vector<int> res;
int m = arr.size();
int n = arr[0].size();
int T = 0;
int B = m-1;
int L = 0;
int R = n-1;
int dir = 0;
while(T <= B && L <= R)
{
if(dir == 0) //left to right(++)
{
for(int i=L; i<=R; i++)
{
//cout<<arr[T][i]<<" ";
res.push_back(arr[T][i]);
}
T++;
//dir = 1;
}
else if(dir == 1) //top to down
{
for(int i = T; i<=B; i++)
{
res.push_back(arr[i][R]);
}
//dir =2;
R--;
}else if(dir == 2) //left to right(--)
{
for(int i=R; i>=L; i--)
{
//cout<<arr[B][i]<<" ";
res.push_back(arr[B][i]);
}
B--;
// dir = 3;
}else if(dir==3)// dir == 3 ----
{
for(int i=B; i>=T; i--)
{
//cout<<arr[i][L]<<" ";
res.push_back(arr[i][L]);
}
L++;
//dir = 0;
}
dir = (dir+1)%4;
}
// cout<<endl;
return res;
}