首页 > 其他 > 详细

leetcode-208-实现前缀树

时间:2019-10-02 16:28:58      阅读:98      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

 

 方法一:

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-实现前缀树

原文:https://www.cnblogs.com/oldby/p/11617600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!