打开主菜单
首页
随机
登录
设置
关于wrc's Wiki
免责声明
wrc's Wiki
搜索
查看“Trie”的源代码
←
Trie
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
[[Category:刷题]] 字典树。 <syntaxhighlight lang=python> class Trie: def __init__(self): self.children = defaultdict(Trie) self.isword = False def insert(self, word: str) -> None: """Inserts a word into the trie.""" t = self for c in word: t = t.children[c] t.isword = True def search(self, word: str) -> bool: """Returns if the word is in the trie.""" t = self for c in word: if c in t.children: t = t.children[c] else: return False return t.isword def startsWith(self, prefix: str) -> bool: """Returns if there is any word in the trie that starts with the given prefix.""" t = self for c in prefix: if c in t.children: t = t.children[c] else: return False return True </syntaxhighlight> == 相关题目 == === [https://leetcode.com/problems/implement-trie-prefix-tree/ LeetCode 208. Implement Trie (Prefix Tree)] ===
返回至
Trie
。