Skip to content

Commit e92c92b

Browse files
committed
一刷212
1 parent 5ad58d9 commit e92c92b

File tree

7 files changed

+141
-31
lines changed

7 files changed

+141
-31
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,12 +1510,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
15101510
|Medium
15111511
|
15121512

1513-
//|{counter:codes}
1514-
//|{leetcode_base_url}/word-search-ii/[212. Word Search II^]
1515-
//|{source_base_url}/_0212_WordSearchII.java[Java]
1516-
//|{doc_base_url}/0212-word-search-ii.adoc[题解]
1517-
//|Hard
1518-
//|
1513+
|{counter:codes}
1514+
|{leetcode_base_url}/word-search-ii/[212. Word Search II^]
1515+
|{source_base_url}/_0212_WordSearchII.java[Java]
1516+
|{doc_base_url}/0212-word-search-ii.adoc[题解]
1517+
|Hard
1518+
|
15191519

15201520
|{counter:codes}
15211521
|{leetcode_base_url}/house-robber-ii/[213. House Robber II^]

docs/0212-word-search-ii.adoc

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,75 @@
11
[#0212-word-search-ii]
2-
= 212. Word Search II
2+
= 212. 单词搜索 II
33

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

6-
Given a 2D board and a list of words from the dictionary, find all words in the board.
6+
给定一个 `m x n` 二维字符网格 `board` 和一个单词(字符串)列表 `words`_返回所有二维网格上的单词_
77

8-
Each word must 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 in a word.
8+
单词必须按照字母顺序,通过 *相邻的单元格* 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
99

10-
10+
*示例 1:*
1111

12-
*Example:*
12+
image::images/0212-01.jpg[{image_attr}]
1313

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:*
17-
*board *= [
18-
['**o**','**a**','a','n'],
19-
['e','**t**','**a**',' e'],
20-
['i','**h**','k','r'],
21-
['i','f','l','v']
22-
]
23-
*words* = `["oath","pea","eat","rain"]`
24-
25-
*Output:* `["eat","oath"]`
26-
----
14+
....
15+
输入:board = [["o","a","a","n"],
16+
["e","t","a","e"],
17+
["i","h","k","r"],
18+
["i","f","l","v"]],
19+
words = ["oath","pea","eat","rain"]
20+
输出:["eat","oath"]
21+
....
2722

28-
23+
*示例 2:*
2924

30-
*Note:*
25+
image::images/0212-02.jpg[{image_attr}]
3126

27+
....
28+
输入:board = [["a","b"],
29+
["c","d"]],
30+
words = ["abcb"]
31+
输出:[]
32+
....
3233

33-
. All inputs are consist of lowercase letters `a-z`.
34-
. The values of `words` are distinct.
34+
*提示:*
3535

36+
* `m == board.length`
37+
* `n == board[i].length`
38+
* `+1 <= m, n <= 12+`
39+
* `board[i][j]` 是一个小写英文字母
40+
* `1 \<= words.length \<= 3 * 10^4^`
41+
* `+1 <= words[i].length <= 10+`
42+
* `words[i]` 由小写英文字母组成
43+
* `words` 中的所有字符串互不相同
3644
3745
46+
== 思路分析
47+
48+
回溯:先根据首字母对单词进行分类,再遍历矩阵,找出首字母对应的坐标点。再对单词列表和坐标点进行回溯匹配。通过 43 / 65 个测试用例,后超时!
3849

3950
[[src-0212]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
4056
[{java_src_attr}]
4157
----
42-
include::{sourcedir}/_0212_WordSearchII.java[tag=answer]
58+
include::{sourcedir}/_0212_WordSearchIi.java[tag=answer]
4359
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0212_WordSearchIi_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
74+
4475

docs/images/0212-01.jpg

13.5 KB
Loading

docs/images/0212-02.jpg

3.7 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ include::0210-course-schedule-ii.adoc[leveloffset=+1]
507507

508508
include::0211-design-add-and-search-words-data-structure.adoc[leveloffset=+1]
509509

510-
// include::0212-word-search-ii.adoc[leveloffset=+1]
510+
include::0212-word-search-ii.adoc[leveloffset=+1]
511511

512512
include::0213-house-robber-ii.adoc[leveloffset=+1]
513513

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,11 @@ endif::[]
963963
|{doc_base_url}/0201-bitwise-and-of-numbers-range.adoc[题解]
964964
|⭕️ 位运算!暴力解法超时!可以利用 stem:[n&(n-1)] 去掉 `n` 的最后一位 `1`,反复计算,直到范围的左边界为止即可得到答案。
965965

966+
|{counter:codes2503}
967+
|{leetcode_base_url}/word-search-ii/[212. 单词搜索 II^]
968+
|{doc_base_url}/0212-word-search-ii.adoc[题解]
969+
|⭕️ 回溯,通过 43 / 65 个测试用例,后超时。
970+
966971
|===
967972

968973
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0212_WordSearchIi {
6+
// tag::answer[]
7+
/**
8+
* 通过 43 / 65 个测试用例。超时!
9+
*
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-06-12 22:53:03
12+
*/
13+
public List<String> findWords(char[][] board, String[] words) {
14+
Arrays.sort(words, String::compareTo);
15+
Map<Character, List<String>> charToWordMap = new HashMap<>();
16+
for (int i = 0; i < words.length; i++) {
17+
String word = words[i];
18+
char c = word.charAt(0);
19+
charToWordMap.computeIfAbsent(c, k -> new ArrayList<>()).add(word);
20+
}
21+
Map<Character, List<int[]>> charToIndexMap = new HashMap<>();
22+
for (int r = 0; r < board.length; r++) {
23+
for (int c = 0; c < board[r].length; c++) {
24+
char ac = board[r][c];
25+
if (charToWordMap.containsKey(ac)) {
26+
charToIndexMap.computeIfAbsent(ac, k -> new ArrayList<>())
27+
.add(new int[]{r, c});
28+
}
29+
}
30+
}
31+
Set<String> result = new HashSet<>();
32+
charToIndexMap.forEach((k, v) -> {
33+
for (int[] idx : v) {
34+
List<String> aWords = charToWordMap.get(k);
35+
for (String aw : aWords) {
36+
dfs(board, idx[0], idx[1], aw, 0, new HashSet<>(), result);
37+
}
38+
}
39+
});
40+
return new ArrayList<>(result);
41+
}
42+
43+
private void dfs(char[][] board, int row, int col,
44+
String word, int idx, Set<List<Integer>> path,
45+
Set<String> result) {
46+
List<Integer> index = Arrays.asList(row, col);
47+
if (path.contains(index)) {
48+
return;
49+
}
50+
if (result.contains(word)) {
51+
return;
52+
}
53+
if (idx == word.length()) {
54+
result.add(word);
55+
return;
56+
}
57+
if (row < 0 || row >= board.length
58+
|| col < 0 || col >= board[0].length) {
59+
return;
60+
}
61+
if (board[row][col] != word.charAt(idx)) {
62+
return;
63+
}
64+
path.add(index);
65+
int[][] options = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
66+
for (int[] option : options) {
67+
int r = row + option[0];
68+
int c = col + option[1];
69+
dfs(board, r, c, word, idx + 1, path, result);
70+
}
71+
path.remove(index);
72+
}
73+
// end::answer[]
74+
}

0 commit comments

Comments
 (0)