Skip to content

Commit 38db720

Browse files
committed
54.螺旋矩阵
1 parent d51a726 commit 38db720

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
42. 接雨水
2323
51. N 皇后
2424
53. 最大子数组和
25+
54. 螺旋矩阵
2526
55. 跳跃游戏
2627
62. 不同路径
2728
64. 最小路径和

leetcode_Java/Solution0054.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// 54. 螺旋矩阵
2+
3+
4+
/*
5+
1、指针视角
6+
left right
7+
↓ ↓
8+
top → 1 2 3 4
9+
5 6 7 8
10+
9 10 11 12
11+
bottom → 13 14 15 16
12+
13+
2、访问顺序:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
14+
15+
3、matrix[行][列]:明确行是谁,列是谁,谁变化
16+
顶行:matrix[top][left → right] 不变的是行,变化的是列,列左到右
17+
右列:matrix[top → bottom][right] 不变的是列,变化的是行,行上到下
18+
底行:matrix[bottom][right → left] 不变的是行,变化的是列,列右到左
19+
左列:matrix[bottom → top][left] 不变的是列,变化的是行,行下到上
20+
21+
4、此解法最容易理解,算法过程
22+
1)先初始化上下左右四个指针
23+
2)开始循环遍历,顺时针遍历一圈,每遍历完一行或一列,该行或该列指针向内移动一步,作为新的可遍历边界
24+
3)只要 上超过下 或 左超过右,说明已经全部遍历完了,可以结束循环了
25+
*/
26+
class Solution {
27+
public List<Integer> spiralOrder(int[][] matrix) {
28+
List<Integer> res = new ArrayList<>();
29+
int row = matrix.length, col = matrix[0].length;
30+
int top = 0, bottom = row - 1, left = 0, right = col - 1;
31+
while (true) {
32+
for (int i = left; i <= right; i++) {
33+
res.add(matrix[top][i]);
34+
}
35+
top++;
36+
if (top > bottom) break;
37+
38+
for (int i = top; i <= bottom; i++) {
39+
res.add(matrix[i][right]);
40+
}
41+
right--;
42+
if (left > right) break;
43+
44+
for (int i = right; i >= left; i--) {
45+
res.add(matrix[bottom][i]);
46+
}
47+
bottom--;
48+
if (top > bottom) break;
49+
50+
for (int i = bottom; i >= top; i--) {
51+
res.add(matrix[i][left]);
52+
}
53+
left++;
54+
if (left > right) break;
55+
}
56+
return res;
57+
}
58+
}
59+
60+
61+
class Solution {
62+
public List<Integer> spiralOrder(int[][] matrix) {
63+
List<Integer> res = new ArrayList<>();
64+
int row = matrix.length, col = matrix[0].length;
65+
int top = 0, bottom = row - 1, left = 0, right = col - 1;
66+
while(left <= right && top <= bottom){
67+
for(int i = left; i <= right; i++){
68+
res.add(matrix[top][i]);
69+
}
70+
top++;
71+
72+
for(int i = top; i <= bottom ; i++){
73+
res.add(matrix[i][right]);
74+
}
75+
right--;
76+
77+
for(int i = right; i >= left && top <= bottom; i--){
78+
res.add(matrix[bottom][i]);
79+
}
80+
bottom--;
81+
82+
for(int i = bottom; i >= top && left <= right; i--){
83+
res.add(matrix[i][left]);
84+
}
85+
left++;
86+
}
87+
return res;
88+
}
89+
}
90+
91+
92+
// “剑指Offer_Java 19.顺时针打印矩阵”
93+
class Solution {
94+
public List<Integer> spiralOrder(int[][] matrix) {
95+
int row = matrix.length;
96+
int col = matrix[0].length;
97+
ArrayList<Integer> ans = new ArrayList<>();
98+
// 计算循环次数
99+
int circle = (Math.min(row, col) - 1) / 2 + 1;
100+
for (int i = 0; i < circle; i++) {
101+
// 添加首行
102+
for (int j = i; j < col - i; j++)
103+
ans.add(matrix[i][j]);
104+
// 添加尾列
105+
for (int k = i + 1; k < row - i; k++)
106+
ans.add(matrix[k][col - i - 1]);
107+
// 添加尾行
108+
for (int m = col - i - 2; (m >= i) && (row - i - 1 != i); m--)
109+
ans.add(matrix[row - i - 1][m]);
110+
// 添加尾列
111+
for (int n = row - i - 2; (n > i) && (col - i - 1 != i); n--)
112+
ans.add(matrix[n][i]);
113+
}
114+
return ans;
115+
}
116+
}

0 commit comments

Comments
 (0)