-
Notifications
You must be signed in to change notification settings - Fork 0
/
059_SpiralMatrixII59.java
68 lines (64 loc) · 1.69 KB
/
059_SpiralMatrixII59.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
/**
* Given a positive integer n, generate a square matrix filled with elements
* from 1 to n2 in spiral order.
*
* Example:
*
* Input: 3
* Output:
* [
* [ 1, 2, 3 ],
* [ 8, 9, 4 ],
* [ 7, 6, 5 ]
* ]
*/
public class SpiralMatrixII59 {
private int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int i = 1;
int d = 0;
int x = 0;
int y = 0;
int END = n * n;
while (i <= END) {
res[x][y] = i;
int nx = x + directions[d][0];
int ny = y + directions[d][1];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || res[nx][ny] != 0) {
d = (d + 1) % 4;
nx = x + directions[d][0];
ny = y + directions[d][1];
}
x = nx;
y = ny;
i++;
}
return res;
}
public int[][] generateMatrix2(int n) {
if (n == 0) return new int[0][0];
int[][] matrix = new int[n][n];
boolean[][] filled = new boolean[n][n];
int[] dx = new int[]{0, 1, 0, -1};
int[] dy = new int[]{1, 0, -1, 0};
int x = 0;
int y = 0;
int d = 0;
for (int i=1; i<=n*n; i++) {
matrix[x][y] = i;
filled[x][y] = true;
int xx = x + dx[d];
int yy = y + dy[d];
if (xx >= 0 && xx < n && yy >= 0 && yy < n && !filled[xx][yy]) {
x = xx;
y = yy;
} else {
d = (d + 1) % 4;
x += dx[d];
y += dy[d];
}
}
return matrix;
}
}