gpt4 book ai didi

python - Leetcode Python 208. 实现Trie(前缀树)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:39:39 24 4
gpt4 key购买 nike

有人能说我的代码有什么问题吗,当我下载特定测试用例时,它通过了除最后一个测试用例之外的所有测试用例,预期输出和实际输出似乎相同,问题是https://leetcode.com/problems/implement-trie-prefix-tree/description/

编辑 1:这是代码:

class Trie:

def __init__(self):
"""
Initialize your data structure here.
"""
self.data = None
self.children = {}
self.isWord = False

def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
if len(word) == 0:
return
if word[0] not in self.children:
self.children[word[0]] = Trie()
self.insertHelper(word[1:], self.children[word[0]])
else:
self.insertHelper(word[1:], self.children[word[0]])

if len(word) == 1:
self.isWord = True

def insertHelper(self, word, trie):
if len(word) == 0:
return

if word[0] not in trie.children:
trie.children[word[0]] = Trie()
trie.insertHelper(word[1:], trie.children[word[0]])
else:
trie.insertHelper(word[1:], trie.children[word[0]])

if len(word) == 1:
trie.isWord = True





def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
if len(word) == 1 and word[0] in self.children and self.isWord:
return True
elif len(word) == 0:
return False

if word[0] in self.children:
return self.searchHelper(word[1:], self.children[word[0]])
else:
return False

def searchHelper(self, word, trie):
if len(word) == 1 and word[0] in trie.children and trie.isWord:
return True
elif len(word) == 0:
return False

if word[0] in trie.children:
return self.searchHelper(word[1:], trie.children[word[0]])
else:
return False



def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
if len(prefix) == 0:
return False
if prefix[0] in self.children:
return self.startsWithHelper(prefix[1:], self.children[prefix[0]])
else:
return False

def startsWithHelper(self, prefix, trie):
if len(prefix) == 0:
return True

if prefix[0] in trie.children:
return trie.startsWithHelper(prefix[1:], trie.children[prefix[0]])
else:
return False

提前致谢。

最佳答案

我注意到的一个怪癖是将空前缀传递给 startsWith()。如果此方法以 Python str 方法 startswith() 为模型,那么我们期望 True:

>>> "apple".startswith("")
True
>>>

但是你的 Trie 在这种情况下返回 False:

>>> t = Trie()
>>> t.insert("apple")
>>> t.startsWith("")
False
>>>

下面是我对您的代码进行的修改,主要是为了理解它,但我也发现您有冗余,尤其是您的Helper 函数。此代码修复了上面提到的怪癖并且是特定于 Python 3 的:

class Trie:

def __init__(self):
self.children = {}
self.isWord = False

def insert(self, word):
"""
Inserts a word into the trie.
:type word: str (or list internally upon recursion)
:rtype: None
"""

if not word:
return

head, *tail = word

if head not in self.children:
self.children[head] = Trie()

trie = self.children[head]

if tail:
trie.insert(tail)
else:
self.isWord = True

def search(self, word):
"""
Returns True if the word is in the trie.
:type word: str (or list internally upon recursion)
:rtype: bool
"""

if not word:
return False

head, *tail = word

if head in self.children:
if not tail and self.isWord:
return True

return self.children[head].search(word[1:])

return False

def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str (or list internally upon recursion)
:rtype: bool
"""

if not prefix:
return True

head, *tail = prefix

if head in self.children:
return self.children[head].startsWith(tail)

return False

关于python - Leetcode Python 208. 实现Trie(前缀树),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53035282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com