leetcode-208-实现前缀树

Posted oldby

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-208-实现前缀树相关的知识,希望对你有一定的参考价值。

题目描述:

技术图片

 

 方法一:

class Trie:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.tree = 

    def insert(self, word: str) -> None:
        """
        Inserts a word into the trie.
        """
        tree = self.tree
        for a in word:
            if a not in tree:
                tree[a]=
            tree = tree[a]
        tree[#] = #

    def search(self, word: str) -> bool:
        """
        Returns if the word is in the trie.
        """
        tree = self.tree
        for a in word:
            if a not in tree: 
                return False
            tree = tree[a]
        if # in tree:
            return True

    def startsWith(self, prefix: str) -> bool:
        """
        Returns if there is any word in the trie that starts with the given prefix.
        """
        tree = self.tree
        for a in prefix:
            if a not in tree:
                return False
            tree = tree[a]
        return True


# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)

 

以上是关于leetcode-208-实现前缀树的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode208. 实现 Trie (前缀树)(相关话题:字典树,前缀树)

[JavaScript 刷题] 树 - 实现前缀树, leetcode 208

[JavaScript 刷题] 树 - 实现前缀树, leetcode 208

LeetCode | 208. 实现 Trie (前缀树)

leetcode 208. 实现 Trie (前缀树)/字典树

[LeetCode] 208. 实现 Trie (前缀树)