/
SpiralMatrix2.java
43 lines (41 loc) · 1.12 KB
/
SpiralMatrix2.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
// https://leetcode.com/problems/spiral-matrix-ii
class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int dir = 0;
int k = 1, i = 0, j = 0;
for (; k <= n * n; k++) {
res[i][j] = k;
if (dir == 0) {
if (j < n - 1 && res[i][j + 1] == 0) {
j++;
} else {
dir++;
i++;
}
} else if (dir == 1) {
if (i < n - 1 && res[i + 1][j] == 0) {
i++;
} else {
dir++;
j--;
}
} else if (dir == 2) {
if (j > 0 && res[i][j - 1] == 0) {
j--;
} else {
dir++;
i--;
}
} else {
if (i > 0 && res[i - 1][j] == 0) {
i--;
} else {
dir = 0;
j++;
}
}
}
return res;
}
}