Skip to content

Commit c98d5dc

Browse files
committed
三刷79
1 parent 873aae2 commit c98d5dc

File tree

6 files changed

+117
-19
lines changed

6 files changed

+117
-19
lines changed

docs/0079-word-search.adoc

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
[#0079-word-search]
2-
= 79. Word Search
2+
= 79. 单词搜索
33

4-
{leetcode}/problems/word-search/[LeetCode - Word Search^]
4+
https://leetcode.cn/problems/word-search/[LeetCode - 79. 单词搜索^]
55

6-
Given a 2D board and a word, find if the word exists in the grid.
6+
给定一个 `m x n` 二维字符网格 `board` 和一个字符串单词 `word`。如果 `word` 存在于网格中,返回 `true` ;否则,返回 `false`
77

8-
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
8+
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
99

10-
*Example:*
10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
board =
15-
[
16-
['A','B','C','E'],
17-
['S','F','C','S'],
18-
['A','D','E','E']
19-
]
20-
21-
Given word = "*ABCCED*", return *true*.
22-
Given word = "*SEE*", return *true*.
23-
Given word = "*ABCB*", return *false*.
24-
----
12+
image::images/0079-01.jpg[{image_attr}]
13+
14+
....
15+
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
16+
输出:true
17+
....
18+
19+
*示例 2:*
20+
21+
image::images/0079-02.jpg[{image_attr}]
22+
23+
....
24+
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
25+
输出:true
26+
....
27+
28+
*示例 3:*
29+
30+
image::images/0079-03.jpg[{image_attr}]
31+
32+
....
33+
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
34+
输出:false
35+
....
36+
37+
*提示:*
38+
39+
* `m == board.length`
40+
* `n = board[i].length`
41+
* `1 \<= m, n \<= 6`
42+
* `1 \<= word.length \<= 15`
43+
* `board``word` 仅由大小写英文字母组成
44+
45+
**进阶:**你可以使用搜索剪枝的技术来优化解决方案,使其在 `board` 更大的情况下可以更快解决问题?
2546

2647
== 思路分析
2748

@@ -49,11 +70,24 @@ include::{sourcedir}/_0079_WordSearch.java[tag=answer]
4970
include::{sourcedir}/_0079_WordSearch_2.java[tag=answer]
5071
----
5172
--
73+
74+
三刷::
75+
+
76+
--
77+
[{java_src_attr}]
78+
----
79+
include::{sourcedir}/_0079_WordSearch_3.java[tag=answer]
80+
----
81+
--
5282
====
5383

84+
5485
== 参考资料
5586

56-
. https://leetcode.cn/problems/word-search/solutions/2361646/79-dan-ci-sou-suo-hui-su-qing-xi-tu-jie-5yui2/?envType=study-plan-v2&envId=selected-coding-interview[79. 单词搜索 - 回溯,清晰图解^]
87+
. https://leetcode.cn/problems/word-search/solutions/2927294/liang-ge-you-hua-rang-dai-ma-ji-bai-jie-g3mmm/[79. 单词搜索 - 极致优化!代码击败接近 100%!^]
88+
. https://leetcode.cn/problems/word-search/solutions/2361646/79-dan-ci-sou-suo-hui-su-qing-xi-tu-jie-5yui2/[79. 单词搜索 - 回溯,清晰图解^]
89+
. https://leetcode.cn/problems/word-search/solutions/411749/shou-hua-tu-jie-79-dan-ci-sou-suo-dfs-si-lu-de-cha/[79. 单词搜索 - 「手画图解」回溯思路的形成与细节^]
90+
. https://leetcode.cn/problems/word-search/solutions/411613/dan-ci-sou-suo-by-leetcode-solution/[79. 单词搜索 - 官方题解^]
5791

5892

5993

docs/images/0079-01.jpg

12.9 KB
Loading

docs/images/0079-02.jpg

12.2 KB
Loading

docs/images/0079-03.jpg

11 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,11 @@ endif::[]
19141914
|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
19151915
|⭕️ 单调栈。哨兵技巧非常巧妙,不仅仅一边可以加哨兵,两边都可以加哨兵。另外,弹出的栈顶元素两边(栈顶元素,当前遍历元素)都是比栈顶低的元素,所以,取栈顶元素和“两边夹层”就是可以得到的最大矩形。
19161916

1917+
|{counter:codes2503}
1918+
|{leetcode_base_url}/word-search/[79. 单词搜索^]
1919+
|{doc_base_url}/0079-word-search.adoc[题解]
1920+
|✅ 回溯。为了尽可能高效,找到就返回。
1921+
19171922
|===
19181923

19191924
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0079_WordSearch_3 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-11-15 08:52:34
9+
*/
10+
public boolean exist(char[][] board, String word) {
11+
for (int c = 0; c < board.length; c++) {
12+
for (int r = 0; r < board[c].length; r++) {
13+
if (board[c][r] == word.charAt(0)) {
14+
boolean found = backtrack(board, word, c, r, 0);
15+
if (found) {
16+
return true;
17+
}
18+
}
19+
}
20+
}
21+
return false;
22+
}
23+
24+
private boolean backtrack(char[][] board, String word,
25+
int column, int row, int index) {
26+
if (index == word.length()) {
27+
return true;
28+
}
29+
if (column < 0 || board.length <= column
30+
|| row < 0 || board[column].length <= row
31+
|| board[column][row] != word.charAt(index)) {
32+
return false;
33+
}
34+
board[column][row] = '.';
35+
// 上
36+
boolean found = backtrack(board, word, column - 1, row, index + 1);
37+
if (found) {
38+
return true;
39+
}
40+
// 下
41+
found = backtrack(board, word, column + 1, row, index + 1);
42+
if (found) {
43+
return true;
44+
}
45+
// 左
46+
found = backtrack(board, word, column, row - 1, index + 1);
47+
if (found) {
48+
return true;
49+
}
50+
// 右
51+
found = backtrack(board, word, column, row + 1, index + 1);
52+
if (found) {
53+
return true;
54+
}
55+
board[column][row] = word.charAt(index);
56+
return false;
57+
}
58+
// end::answer[]
59+
}

0 commit comments

Comments
 (0)