@@ -125,7 +125,7 @@ def search(self, word: str) -> bool:
125
125
return False # 直接返回 False
126
126
cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
127
127
128
- return cur is not None and cur.isEnd # 判断当前节点是否为空,并且是否有单词结束标记
128
+ return cur.isEnd # 判断是否有单词结束标记
129
129
```
130
130
131
131
#### 2.3.2 字典树的查找前缀操作
@@ -140,7 +140,7 @@ def startsWith(self, prefix: str) -> bool:
140
140
if ch not in cur.children: # 如果当前节点的子节点中,不存在键为 ch 的节点
141
141
return False # 直接返回 False
142
142
cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
143
- return cur is not None # 判断当前节点是否为空,不为空则查找成功
143
+ return True # 查找成功
144
144
```
145
145
146
146
## 3. 字典树的实现代码
@@ -175,7 +175,7 @@ class Trie: # 字典树
175
175
return False # 直接返回 False
176
176
cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
177
177
178
- return cur is not None and cur.isEnd # 判断当前节点是否为空,并且是否有单词结束标记
178
+ return cur.isEnd # 判断是否有单词结束标记
179
179
180
180
# 查找字典树中是否存在一个前缀
181
181
def startsWith (self , prefix : str ) -> bool :
@@ -184,7 +184,7 @@ class Trie: # 字典树
184
184
if ch not in cur.children: # 如果当前节点的子节点中,不存在键为 ch 的节点
185
185
return False # 直接返回 False
186
186
cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
187
- return cur is not None # 判断当前节点是否为空,不为空则查找成功
187
+ return True # 查找成功
188
188
```
189
189
190
190
## 4. 字典树的算法分析
0 commit comments