-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrixII.java
More file actions
97 lines (84 loc) · 2.3 KB
/
SpiralMatrixII.java
File metadata and controls
97 lines (84 loc) · 2.3 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
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
94
95
96
97
package leetcode;
/**
* SpiralMatrixII
* https://leetcode-cn.com/problems/spiral-matrix-ii/
* 59. 螺旋矩阵 II
* https://leetcode-cn.com/problems/spiral-matrix-ii/solution/mo-ni-bian-jie-pan-duan-by-oshdyr-0des/
*
* @since 2021-03-16
*/
public class SpiralMatrixII {
public static void main(String[] args) {
SpiralMatrixII sol = new SpiralMatrixII();
int[][] res = sol.generateMatrix(4);
for (int[] row : res) {
for (int value : row) {
System.out.print(value + ", ");
}
System.out.println();
}
int[][] res2 = sol.generateMatrix(1);
for (int[] row : res2) {
for (int value : row) {
System.out.print(value + ", ");
}
System.out.println();
}
int[][] res3 = sol.generateMatrix(3);
for (int[] row : res3) {
for (int value : row) {
System.out.print(value + ", ");
}
System.out.println();
}
int[][] res4 = sol.generateMatrix(2);
for (int[] row : res4) {
for (int value : row) {
System.out.print(value + ", ");
}
System.out.println();
}
}
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int value = 1;
int x = 0;
int y = 0;
int pad_right = 1;
int pad_down = 1;
int pad_left = 1;
int pad_up = 2;
while (true) {
if (y >= n - pad_right) {
break;
}
for (;y < n - pad_right; y++) {
res[x][y] = value++;
}
pad_right++;
if (x >= n - pad_down) {
break;
}
for (; x < n - pad_down; x++) {
res[x][y] = value++;
}
pad_down++;
if (y < pad_left) {
break;
}
for (; y >= pad_left; y--) {
res[x][y] = value++;
}
pad_left++;
if (x < pad_up) {
break;
}
for (; x >= pad_up; x--) {
res[x][y] = value++;
}
pad_up++;
}
res[x][y] = value;
return res;
}
}