-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpiralMatrixII.java
More file actions
59 lines (54 loc) · 1.5 KB
/
SpiralMatrixII.java
File metadata and controls
59 lines (54 loc) · 1.5 KB
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
package leetcode;
/**
* @author eko
*
* 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 SpiralMatrixII {
public static void main(String[] args) {
int[][] matrix = generateMatrix(3);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}
}
public static int[][] generateMatrix(int n) {
if (n == 0) return new int[0][0];
int[][] matrix = new int[n][n];
int index = n;
int count = 1;
int layer = 0;
while (count <= n * n) {
if (index == 1) {
matrix[layer][layer] = count++;
} else {
for (int i = 0; i < index - 1; i++) {
matrix[layer][layer + i] = count++;
}
for (int i = 0; i < index - 1; i++) {
matrix[layer + i][layer + index - 1] = count++;
}
for (int i = index - 1; i > 0; i--) {
matrix[layer + index - 1][layer + i] = count++;
}
for (int i = index - 1; i > 0; i--) {
matrix[layer + i][layer] = count++;
}
}
index -= 2;
layer++;
}
return matrix;
}
}