Skip to content

Commit 36b78da

Browse files
committed
二刷54
1 parent 3e05974 commit 36b78da

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

docs/0054-spiral-matrix.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,36 @@ Given a matrix of _m_ x _n_ elements (_m_ rows, _n_ columns), return all element
3030
*Output:* [1,2,3,4,8,12,11,10,9,5,6,7]
3131
----
3232

33+
== 思路分析
34+
35+
从回溯思想得到启发,使用递归来逐层推进。每次方法调用只负责指定层的遍历,向里推进层次的工作,交给递归来完成。这样避免了复杂的判断。
36+
37+
image::images/0054-01.png[{image_attr}]
3338

3439
[[src-0054]]
40+
[tabs]
41+
====
42+
一刷::
43+
+
44+
--
3545
[{java_src_attr}]
3646
----
3747
include::{sourcedir}/_0054_SpiralMatrix.java[tag=answer]
3848
----
49+
--
50+
51+
二刷::
52+
+
53+
--
54+
[{java_src_attr}]
55+
----
56+
include::{sourcedir}/_0054_SpiralMatrix_2.java[tag=answer]
57+
----
58+
--
59+
====
60+
61+
== 参考资料
62+
63+
. https://leetcode.cn/problems/spiral-matrix/solutions/275393/luo-xuan-ju-zhen-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[54. 螺旋矩阵 - 官方题解^]
64+
. https://leetcode.cn/problems/spiral-matrix/solutions/2362055/54-luo-xuan-ju-zhen-mo-ni-qing-xi-tu-jie-juvi/?envType=study-plan-v2&envId=selected-coding-interview[54. 螺旋矩阵 - 模拟,清晰图解^]
3965

docs/images/0054-01.png

37.8 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@
555555
|{doc_base_url}/0142-linked-list-cycle-ii.adoc[题解]
556556
|快慢指针
557557

558+
|{counter:codes}
559+
|{leetcode_base_url}/spiral-matrix/[54. Spiral Matrix^]
560+
|{doc_base_url}/0054-spiral-matrix.adoc[题解]
561+
|从回溯得到启发,使用递归来完成层次的遍历。
562+
558563
|===
559564

560565
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0054_SpiralMatrix.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class _0054_SpiralMatrix {
4242
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Spiral Matrix.
4343
*
4444
* Memory Usage: 34.4 MB, less than 100.00% of Java online submissions for Spiral Matrix.
45+
*
46+
* @author D瓜哥 · https://www.diguage.com
47+
* @since 2019-10-26 00:51:20
4548
*/
4649
public List<Integer> spiralOrder(int[][] matrix) {
4750
if (Objects.isNull(matrix) || matrix.length == 0) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0054_SpiralMatrix_2 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2024-09-14 17:42:39
12+
*/
13+
public List<Integer> spiralOrder(int[][] matrix) {
14+
int row = matrix.length;
15+
int column = matrix[0].length;
16+
List<Integer> result = new ArrayList<>(row * column);
17+
bfs(matrix, result, 0, 0, row, column);
18+
return result;
19+
}
20+
21+
private void bfs(int[][] matrix, List<Integer> result,
22+
int row, int column,
23+
int rLen, int cLen) {
24+
if (rLen <= 0 || cLen <= 0) {
25+
return;
26+
}
27+
for (int i = column; i < column + cLen; i++) {
28+
result.add(matrix[row][i]);
29+
}
30+
for (int i = row + 1; i < row + rLen; i++) {
31+
result.add(matrix[i][column + cLen - 1]);
32+
}
33+
// 不想增加复杂判断了,数量足够就直接返回
34+
// 如果不返回,在最后只剩下一层且有多个元素时,中间元素会被重复添加
35+
if (result.size() == matrix.length * matrix[0].length) {
36+
return;
37+
}
38+
for (int i = column + cLen - 2; i >= column; i--) {
39+
result.add(matrix[row + rLen - 1][i]);
40+
}
41+
for (int i = row + rLen - 2; i > row; i--) {
42+
result.add(matrix[i][column]);
43+
}
44+
bfs(matrix, result, row + 1, column + 1, rLen - 2, cLen - 2);
45+
}
46+
// end::answer[]
47+
}

0 commit comments

Comments
 (0)