-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
41 lines (34 loc) · 1.28 KB
/
SpiralMatrix.java
File metadata and controls
41 lines (34 loc) · 1.28 KB
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
import java.util.*;
public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix.length == 0) return res;
int top_border = 0;
int right_border = matrix[0].length - 1;
int bottom_border = matrix.length - 1;
int left_border = 0;
while (top_border <= bottom_border && left_border <= right_border) {
if (left_border > right_border) return res;
for (int i = left_border; i <= right_border; i++) {
res.add(matrix[top_border][i]);
}
top_border++;
if (top_border > bottom_border) return res;
for (int i = top_border; i <= bottom_border; i++) {
res.add(matrix[i][right_border]);
}
right_border--;
if (left_border > right_border) return res;
for (int i = right_border; i >= left_border; i--) {
res.add(matrix[bottom_border][i]);
}
bottom_border--;
if (top_border > bottom_border) return res;
for (int i = bottom_border; i >= top_border; i--) {
res.add(matrix[i][left_border]);
}
left_border++;
}
return res;
}
}