-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral Order Matrix
More file actions
37 lines (33 loc) · 840 Bytes
/
Copy pathSpiral Order Matrix
File metadata and controls
37 lines (33 loc) · 840 Bytes
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
public class Solution {
// DO NOT MODIFY THE LIST
public ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> a) {
ArrayList<Integer> result = new ArrayList<Integer>();
// Populate result;
int T=0, B=a.size()-1, L=0, R=a.get(0).size()-1, direction=0;
while(T<=B && L<=R)
{
if(direction==0){
for(int i=L;i<=R;i++)
result.add(a.get(T).get(i));
T++;
}
if(direction==1){
for(int i=T; i<=B;i++)
result.add(a.get(i).get(R));
R--;
}
if(direction==2){
for(int i=R; i>=L;i--)
result.add(a.get(B).get(i));
B--;
}
if(direction==3){
for(int i=B; i>=T;i--)
result.add(a.get(i).get(L));
L++;
}
direction=(direction+1)%4;
}
return result;
}
}