-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpiralMatrix.java
34 lines (33 loc) · 1.19 KB
/
SpiralMatrix.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
import java.util.ArrayList;
import java.util.List;
/**
* 54. Spiral Matrix
* 数组。
* @author LBW
*/
public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix.length == 0)
return new ArrayList<>();
int m = matrix.length, n = matrix[0].length;
int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1,}, {-1, 0}}; // indicate four directions
int idx = 0; // indicate current direction.(right)
boolean[][] isVisited = new boolean[m][n];
List<Integer> res = new ArrayList<>();
int i = 0, j = 0;
for (int k = 0; k < m * n; k++) {
res.add(matrix[i][j]);
isVisited[i][j] = true;
if (i + directions[idx][0] >= 0 && i + directions[idx][0] < m && j + directions[idx][1] >= 0 && j + directions[idx][1] < n && !isVisited[i + directions[idx][0]][j + directions[idx][1]]) {
i += directions[idx][0];
j += directions[idx][1];
}
else {
idx = (idx + 1) % 4; //change direction.
i += directions[idx][0];
j += directions[idx][1];
}
}
return res;
}
}