We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ddce646 commit 2a730e0Copy full SHA for 2a730e0
ppt/算法讲解044【必备】前缀树原理和代码详解.pptx
38.4 KB
src/class044/Code01_TrieTree.java
@@ -42,6 +42,8 @@ public void insert(String word) {
42
node.end++;
43
}
44
45
+ // 如果之前word插入过前缀树,那么此时删掉一次
46
+ // 如果之前word没有插入过前缀树,那么什么也不做
47
public void erase(String word) {
48
if (countWordsEqualTo(word) > 0) {
49
TrieNode node = root;
@@ -58,6 +60,7 @@ public void erase(String word) {
58
60
59
61
62
63
+ // 查询前缀树里,word单词出现了几次
64
public int countWordsEqualTo(String word) {
65
66
for (int i = 0, path; i < word.length(); i++) {
@@ -70,6 +73,7 @@ public int countWordsEqualTo(String word) {
70
73
return node.end;
71
74
72
75
76
+ // 查询前缀树里,有多少单词以pre做前缀
77
public int countWordsStartingWith(String pre) {
78
79
for (int i = 0, path; i < pre.length(); i++) {
0 commit comments