-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrixii.java
More file actions
114 lines (110 loc) · 3.58 KB
/
Copy pathSpiralMatrixii.java
File metadata and controls
114 lines (110 loc) · 3.58 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Source : https://leetcode.com/problems/spiral-matrix-ii/
// Author : zhoutianbin
// Date : 2021-03-16
/*****************************************************************************************************
*
* Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral
* order.
*
* Example 1:
*
* Input: n = 3
* Output: [[1,2,3],[8,9,4],[7,6,5]]
*
* Example 2:
*
* Input: n = 1
* Output: [[1]]
*
* Constraints:
*
* 1 <= n <= 20
******************************************************************************************************/
package SpiralMatrix2;
public class SpiralMatrixii {
// 模拟旋转法,时间复杂度 O(n^2), 空间复杂度O(1)
public int[][] generateMatrix(int n) {
int maxNum = n * n;
int curNum = 1;
int[][] matrix = new int[n][n];
int row = 0;
int column = 0;
// core 这里的顺序不能变,顺时针的顺序:右 -- 下 -- 左 -- 上
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int directionIndex = 0;
while (curNum <= maxNum) {
matrix[row][column] = curNum;
curNum++;
int nextRow = row + directions[directionIndex][0];
int nextColumn = column + directions[directionIndex][1];
// core 核心代码,这里进行顺时针旋转的逻辑,最后一个判断条件很关键,用来判断在旋转时是否碰到了其他边
if (nextRow < 0 || nextRow >= n || nextColumn < 0 || nextColumn >= n || matrix[nextRow][nextColumn] != 0) {
directionIndex = (directionIndex + 1) % 4;
}
row = row + directions[directionIndex][0];
column = column + directions[directionIndex][1];
}
return matrix;
}
// 按层遍历
public int[][] generateMatrix2(int n) {
int left = 0;
int top = 0;
int right = n - 1;
int bottom = n - 1;
int[][] res = new int[n][n];
int currNum = 1;
while (top < n && left < n) {
// 分四次画出一个环
for (int i = left; i <= right; i++) {
res[top][i] = currNum++;
}
for (int i = top + 1; i < bottom; i++) {
res[i][right] = currNum++;
}
// core 防止重复赋值
if (left < right) {
for (int i = right; i >= left; i--) {
res[bottom][i] = currNum++;
}
}
for (int i = bottom - 1; i > top; i--) {
res[i][left] = currNum++;
}
left++;
top++;
bottom--;
right--;
}
return res;
}
// core 也是按层画的思想,不过代码更加简洁
public int[][] generateMatrix3(int n) {
int left = 0;
int top = 0;
int right = n - 1;
int bottom = n - 1;
int[][] res = new int[n][n];
int currNum = 1;
while (currNum <= n * n) {
// 分四次画出一个环
for (int i = left; i <= right; i++) {
res[top][i] = currNum++;
}
top++;
for (int i = top; i <= bottom; i++) {
res[i][right] = currNum++;
}
right--;
for (int i = right; i >= left; i--) {
res[bottom][i] = currNum++;
}
bottom--;
for (int i = bottom; i >= top; i--) {
res[i][left] = currNum++;
}
left++;
}
return res;
}
}