Skip to content

Commit 013954a

Browse files
committed
59.螺旋矩阵II,四指针
1 parent e3afd38 commit 013954a

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
54. 螺旋矩阵
3030
55. 跳跃游戏
3131
56. 合并区间
32+
59. 螺旋矩阵 II
3233
62. 不同路径
3334
64. 最小路径和
3435
69. x 的平方根

leetcode_Java/DoneType.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
48. 旋转图像(置换)
147147
54. 螺旋矩阵(四指针)
148148
56. 合并区间(二维数组排序)
149+
59. 螺旋矩阵 II(四指针)
149150
75. 颜色分类(单指针,双指针)
150151
88. 合并两个有序数组(排序,双指针)
151152
128. 最长连续序列(集合,排序)

leetcode_Java/Solution0054.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
/*
5+
思路同“59.螺旋矩阵II”
6+
57
1、指针视角
68
left right
79
↓ ↓

leetcode_Java/Solution0059.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 59. 螺旋矩阵 II
2+
3+
4+
/*
5+
思路同“54.螺旋矩阵”
6+
*/
7+
class Solution {
8+
public int[][] generateMatrix(int n) {
9+
int[][] matrix = new int[n][n];
10+
int left = 0, right = n - 1, top = 0, bottom = n - 1, num = 1;
11+
while (true) {
12+
for (int i = left; i <= right; i++) {
13+
matrix[top][i] = num++;
14+
}
15+
top++;
16+
if (top > bottom) break;
17+
18+
for (int i = top; i <= bottom; i++) {
19+
matrix[i][right] = num++;
20+
}
21+
right--;
22+
if (left > right) break;
23+
24+
for (int i = right; i >= left; i--) {
25+
matrix[bottom][i] = num++;
26+
}
27+
bottom--;
28+
if (top > bottom) break;
29+
30+
for (int i = bottom; i >= top; i--) {
31+
matrix[i][left] = num++;
32+
}
33+
left++;
34+
if (left > right) break;
35+
}
36+
return matrix;
37+
}
38+
}

0 commit comments

Comments
 (0)