-
Notifications
You must be signed in to change notification settings - Fork 20
/
SpiralMatrixII.java
executable file
·61 lines (53 loc) · 1.5 KB
/
SpiralMatrixII.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
/**
* Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
* <p>
* For example,
* Given n = 3,
* <p>
* You should return the following matrix:
* [
* [ 1, 2, 3 ],
* [ 8, 9, 4 ],
* [ 7, 6, 5 ]
* ]
* <p>
* Accepted.
*/
public class SpiralMatrixII {
public int[][] generateMatrix(int n) {
if (n <= 0) {
return new int[][]{};
}
if (n == 1) {
return new int[][]{{1}};
}
int[][] matrix = new int[n][n];
int centerX = n % 2 == 0 ? (n - 1) / 2 : n / 2;
int centerY = n % 2 == 0 ? (n - 1) / 2 : n / 2;
int i = 0, j = 0, depth = 0, result = 1;
while (i <= centerX && j <= centerY && depth <= centerX && depth <= centerY) {
for (j = depth, i = depth; j < matrix[0].length - depth; j++) {
if (matrix[i][j] == 0) {
matrix[i][j] = result++;
}
}
for (j--, i++; i < matrix.length - depth; i++) {
if (matrix[i][j] == 0) {
matrix[i][j] = result++;
}
}
for (i--, j--; j >= depth; j--) {
if (matrix[i][j] == 0) {
matrix[i][j] = result++;
}
}
for (j++, i--; i > depth; i--) {
if (matrix[i][j] == 0) {
matrix[i][j] = result++;
}
}
depth++;
}
return matrix;
}
}