-
Notifications
You must be signed in to change notification settings - Fork 22
/
Spiral Matrix.java
executable file
·93 lines (78 loc) · 2.28 KB
/
Spiral Matrix.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
M
1528298716
tags: Array, Enumeration
从(0,0)坐标, 走完spiral matrix, 把结果存在list里.
#### DX, DY
- Basic implementation, array, enumeration
- 写一下position前进的方向: RIGHT->DOWN->LEFT->UP
- 用一个direction status 确定方向
- 写一个compute direction function 改变方向 `(direction + 1) % 4`
- `boolean[][] visited` 来track走过的地方
```
/*
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
*/
/*
Thoughts:
- Keep visited
- keep moving until hit visited, then turn
*/class Solution {
int[] dx = {0, 1, 0, -1}; // RIGHT->DOWN->LEFT->UP
int[] dy = {1, 0, -1, 0};
public List<Integer> spiralOrder(int[][] matrix) {
// check edge case
List<Integer> rst = new ArrayList<>();
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return rst;
}
int m = matrix.length;
int n = matrix[0].length;
// handle single col case
if (n == 1) {
for (int i = 0; i < m; i++) {
rst.add(matrix[i][0]);
}
return rst;
}
// construct boolean visited[][], dx{}, dy{}
boolean[][] visited = new boolean[m][n];
// while keep moving until count == m*n
int i = 0, x = 0, y = 0;
int direction = 0;
while (i < m * n) {
i++;
rst.add(matrix[x][y]);
visited[x][y] = true;
// compute x/y based on current direction
direction = computeDirection(visited, x, y, direction);
x += dx[direction];
y += dy[direction];
}
return rst;
}
private int computeDirection(boolean[][] visited, int x, int y, int currDirection) {
int nextX = x + dx[currDirection];
int nextY = y + dy[currDirection];
if (nextX >= 0 && nextX < visited.length && nextY >= 0 && nextY < visited[0].length && !visited[nextX][nextY]) {
return currDirection;
}
return (currDirection + 1) % 4;
}
}
```