-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterviewbit-arrays-spiral-order-matrix-i.java
41 lines (41 loc) · 1.42 KB
/
interviewbit-arrays-spiral-order-matrix-i.java
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
public class Solution {
// DO NOT MODIFY THE LIST
public ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> a) {
ArrayList<Integer> result = new ArrayList<Integer>();
int top = 0;
int left = 0;
int right = a.get(0).size() - 1;
int bottom = a.size() - 1;
String[] directions = {"Right", "Down", "Left", "Up"};
int direction = 0;
while (left <= right && top <= bottom) {
switch (directions[direction]) {
case "Right":
for (int i = left; i <= right; i++) {
result.add(a.get(top).get(i));
}
top++;
break;
case "Down":
for (int i = top; i <= bottom; i++) {
result.add(a.get(i).get(right));
}
right--;
break;
case "Left":
for (int i = right; i >= left; i--) {
result.add(a.get(bottom).get(i));
}
bottom--;
break;
case "Up":
for (int i = bottom; i >= top; i--) {
result.add(a.get(i).get(left));
}
left++;
}
direction = (direction + 1) % 4;
}
return result;
}
}