Skip to content

Commit ece6197

Browse files
committed
三刷54
1 parent f649951 commit ece6197

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

docs/0054-spiral-matrix.adoc

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
[#0054-spiral-matrix]
2-
= 54. Spiral Matrix
2+
= 54. 螺旋矩阵
33

4-
{leetcode}/problems/spiral-matrix/[LeetCode - Spiral Matrix^]
4+
https://leetcode.cn/problems/spiral-matrix/[LeetCode - 54. 螺旋矩阵 ^]
55

6-
Given a matrix of _m_ x _n_ elements (_m_ rows, _n_ columns), return all elements of the matrix in spiral order.
6+
给你一个 `m``n` 列的矩阵 `matrix` ,请按照 *顺时针螺旋顺序*,返回矩阵中的所有元素。
77

8-
*Example 1:*
8+
*示例 1:*
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:*
13-
[
14-
[ 1, 2, 3 ],
15-
[ 4, 5, 6 ],
16-
[ 7, 8, 9 ]
17-
]
18-
*Output:* [1,2,3,6,9,8,7,4,5]
19-
----
10+
image::images/0054-01.jpg[{image_attr}]
2011

21-
*Example 2:*
22-
[subs="verbatim,quotes,macros"]
23-
----
24-
*Input:*
25-
[
26-
[1, 2, 3, 4],
27-
[5, 6, 7, 8],
28-
[9,10,11,12]
29-
]
30-
*Output:* [1,2,3,4,8,12,11,10,9,5,6,7]
31-
----
12+
....
13+
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
14+
输出:[1,2,3,6,9,8,7,4,5]
15+
....
16+
17+
*示例 2:*
18+
19+
image::images/0054-02.jpg[{image_attr}]
20+
21+
....
22+
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
23+
输出:[1,2,3,4,8,12,11,10,9,5,6,7]
24+
....
25+
26+
*提示:*
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `+1 <= m, n <= 10+`
31+
* `+-100 <= matrix[i][j] <= 100+`
3232
3333
== 思路分析
3434

@@ -56,6 +56,15 @@ include::{sourcedir}/_0054_SpiralMatrix.java[tag=answer]
5656
include::{sourcedir}/_0054_SpiralMatrix_2.java[tag=answer]
5757
----
5858
--
59+
60+
三刷::
61+
+
62+
--
63+
[{java_src_attr}]
64+
----
65+
include::{sourcedir}/_0054_SpiralMatrix_3.java[tag=answer]
66+
----
67+
--
5968
====
6069

6170
== 参考资料

docs/images/0054-01.jpg

9.92 KB
Loading

docs/images/0054-02.jpg

13.8 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ endif::[]
235235
|{doc_base_url}/0028-find-the-index-of-the-first-occurrence-in-a-string.adoc[题解]
236236
|✅ 暴力破解。更有的解法是 KMP 算法、Boyer-Moore 算法、Sunday 算法等算法。
237237

238+
|{counter:codes2503}
239+
|{leetcode_base_url}/spiral-matrix/[54. Spiral Matrix^]
240+
|{doc_base_url}/0054-spiral-matrix.adoc[题解]
241+
|✅ 使用递归来推进层级。注意处理细节。另外,在“上”和“右”能覆盖“全部”(比如一行多列或者多行一列)时,才能在“下”之前根据长度返回。
242+
238243
|===
239244
˚
240245
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0054_SpiralMatrix_3 {
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-12 23:18:24
10+
*/
11+
public List<Integer> spiralOrder(int[][] matrix) {
12+
List<Integer> result = new ArrayList<>(matrix.length * matrix[0].length);
13+
bfs(matrix, 0, 0, matrix.length - 1, matrix[0].length - 1, result);
14+
return result;
15+
}
16+
17+
private void bfs(int[][] matrix,
18+
int columnStart, int rowStart,
19+
int columnEnd, int rowEnd,
20+
List<Integer> result) {
21+
if (columnStart > columnEnd || rowStart > rowEnd) {
22+
return;
23+
}
24+
// 上
25+
for (int i = rowStart; i <= rowEnd; i++) {
26+
result.add(matrix[columnStart][i]);
27+
}
28+
// 右
29+
for (int i = columnStart + 1; i <= columnEnd; i++) {
30+
result.add(matrix[i][rowEnd]);
31+
}
32+
if (result.size() == matrix.length * matrix[0].length) {
33+
return;
34+
}
35+
// 下
36+
for (int i = rowEnd - 1; i >= rowStart; i--) {
37+
result.add(matrix[columnEnd][i]);
38+
}
39+
// 左
40+
for (int i = columnEnd - 1; i > columnStart; i--) {
41+
result.add(matrix[i][rowStart]);
42+
}
43+
bfs(matrix, columnStart + 1, rowStart + 1,
44+
columnEnd - 1, rowEnd - 1, result);
45+
}
46+
// end::answer[]
47+
48+
public static void main(String[] args) {
49+
new _0054_SpiralMatrix_3().spiralOrder(new int[][]{
50+
{1, 2, 3, 4},
51+
{5, 6, 7, 8},
52+
{9, 10, 11, 12},
53+
});
54+
}
55+
}

0 commit comments

Comments
 (0)