Skip to content

Commit 242fd6b

Browse files
committed
一刷211
1 parent 33e0242 commit 242fd6b

File tree

5 files changed

+118
-7
lines changed

5 files changed

+118
-7
lines changed

README.adoc

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

1506-
//|{counter:codes}
1507-
//|{leetcode_base_url}/design-add-and-search-words-data-structure/[211. 添加与搜索单词 - 数据结构设计]
1508-
//|{source_base_url}/_0211_DesignAddAndSearchWordsDataStructure.java[Java]
1509-
//|{doc_base_url}/0211-design-add-and-search-words-data-structure.adoc[题解]
1510-
//|Medium
1511-
//|
1506+
|{counter:codes}
1507+
|{leetcode_base_url}/design-add-and-search-words-data-structure/[211. 添加与搜索单词 - 数据结构设计]
1508+
|{source_base_url}/_0211_DesignAddAndSearchWordsDataStructure.java[Java]
1509+
|{doc_base_url}/0211-design-add-and-search-words-data-structure.adoc[题解]
1510+
|Medium
1511+
|
15121512

15131513
//|{counter:codes}
15141514
//|{leetcode_base_url}/word-search-ii/[212. Word Search II^]

docs/0211-design-add-and-search-words-data-structure.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ wordDictionary.search("b.."); // 返回 True
4141
4242
== 思路分析
4343

44+
前缀树。需要处理通配符的情况,尤其要注意结尾字符的处理。
4445

4546
[[src-0211]]
4647
[tabs]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ include::0209-minimum-size-subarray-sum.adoc[leveloffset=+1]
504504

505505
include::0210-course-schedule-ii.adoc[leveloffset=+1]
506506

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

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

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ endif::[]
553553
|{doc_base_url}/0697-degree-of-an-array.adoc[题解]
554554
|✅
555555

556+
|{counter:codes2503}
557+
|{leetcode_base_url}/design-add-and-search-words-data-structure/[211. 添加与搜索单词 - 数据结构设计]
558+
|{doc_base_url}/0211-design-add-and-search-words-data-structure.adoc[题解]
559+
|✅ 前缀树。需要处理通配符的情况,尤其要注意结尾字符的处理。
560+
556561
|===
557562

558563
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0211_DesignAddAndSearchWordsDataStructure {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-30 00:08:59
9+
*/
10+
static class WordDictionary {
11+
private WordDictionary[] letters;
12+
private boolean end = false;
13+
14+
public WordDictionary() {
15+
letters = new WordDictionary[26];
16+
}
17+
18+
public void addWord(String word) {
19+
char[] chars = word.toCharArray();
20+
WordDictionary current = this;
21+
for (int i = 0; i < chars.length; i++) {
22+
if (current.letters[chars[i] - 'a'] == null) {
23+
current.letters[chars[i] - 'a'] = new WordDictionary();
24+
}
25+
current = current.letters[chars[i] - 'a'];
26+
if (i == chars.length - 1) {
27+
current.end = true;
28+
}
29+
}
30+
}
31+
32+
public boolean search(String word) {
33+
char[] chars = word.toCharArray();
34+
return backtrack(chars, 0, this);
35+
}
36+
37+
private boolean backtrack(char[] chars, int index,
38+
WordDictionary current) {
39+
if (index == chars.length) {
40+
return true;
41+
}
42+
if (chars[index] == '.') {
43+
for (int i = 0; i < 26; i++) {
44+
if (current.letters[i] == null) {
45+
continue;
46+
}
47+
if (index == chars.length - 1) {
48+
if (current.letters[i].end) {
49+
return true;
50+
} else {
51+
continue;
52+
}
53+
}
54+
boolean ok = backtrack(chars, index + 1, current.letters[i]);
55+
if (ok) {
56+
return true;
57+
}
58+
}
59+
return false;
60+
} else {
61+
WordDictionary next = current.letters[chars[index] - 'a'];
62+
if (next == null) {
63+
return false;
64+
} else {
65+
if (index == chars.length - 1) {
66+
return next.end;
67+
}
68+
return backtrack(chars, index + 1, next);
69+
}
70+
}
71+
}
72+
}
73+
74+
// end::answer[]
75+
public static void main(String[] args) {
76+
WordDictionary dict = new WordDictionary();
77+
// "addWord","addWord","addWord","addWord","search","search","addWord","search","search","search","search","search","search"
78+
// ["at"], ["and"], ["an"], ["add"], ["a"], [".at"], ["bat"], [".at"], ["an."], ["a.d."], ["b."], ["a.d"], ["."]
79+
dict.addWord("at");
80+
dict.addWord("and");
81+
dict.addWord("an");
82+
dict.addWord("add");
83+
dict.search("a");
84+
dict.search(".at");
85+
dict.search("bat");
86+
dict.search(".at");
87+
dict.search("an.");
88+
dict.search("a.d.");
89+
dict.search("b.");
90+
dict.search("a.d");
91+
dict.search(".");
92+
// "addWord","addWord","search","search","search","search","search","search","search","search"
93+
// ["a"], ["ab"], ["a"], ["a."], ["ab"], [".a"] ,[".b"], ["ab."], ["."], [".."]
94+
// dict.addWord("a");
95+
// dict.addWord("ab");
96+
// dict.search("a");
97+
// dict.search("a.");
98+
// dict.search("ab");
99+
// dict.search(".a");
100+
// dict.search(".b");
101+
// dict.search("ab.");
102+
// dict.search(".");
103+
// dict.search("..");
104+
}
105+
}

0 commit comments

Comments
 (0)