leetcode—前缀树

tire是一种 N 叉树,前缀树,字典树

前缀树是N叉树的一种特殊形式。通常来说,一个前缀树是用来存储字符串的。前缀树的每一个节点代表一个字符串(前缀)。每一个节点会有多个子节点,通往不同子节点的路径上有着不同的字符。子节点代表的字符串是由节点本身的原始字符串,以及通往该子节点路径上所有的字符组成的。

  • 208 实现 Trie (前缀树)
  • 211 添加与搜索单词 - 数据结构设计
  • 421 数组中两个数的最大异或值

208 实现 Trie (前缀树)

https://leetcode-cn.com/problems/implement-trie-prefix-tree/

class Trie:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        # 构建一个字典表示字典树
        self.root = {}
        # 一个单词的结束字符
        self.end = 'end'


    def insert(self, word: str) -> None:
        """
        Inserts a word into the trie.
        """
        curNode = self.root
        for c in word:
            if c not in curNode:
                curNode[c] = {}
            curNode = curNode[c]
        curNode[self.end] = True


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


    def startsWith(self, prefix: str) -> bool:
        """
        Returns if there is any word in the trie that starts with the given prefix.
        """
        curNode = self.root
        for c in prefix:
            if c not in curNode:
                return False
            curNode = curNode[c]
        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)

211 添加与搜索单词 - 数据结构设计

https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/

class WordDictionary:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = {}
        self.end = 'end'


    def addWord(self, word: str) -> None:
        """
        Adds a word into the data structure.
        """
        curNode = self.root
        for c in word:
            if c not in curNode:
                curNode[c] = {}
            curNode = curNode[c]
        curNode[self.end] = {}


    def search(self, word: str) -> bool:
        """
        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
        """
        # 不同于基本的字典树的区别就是这里的'.'可以代表任意一个字母
        # 如果出现'.',递归进行搜索(dfs)
        def helper(word, tree):
            if not word:
                if self.end in tree:
                    return True
                return False

            if word[0] == '.':
                for t in tree:
                    if helper(word[1:], tree[t]):
                        return True
            elif word[0] in tree:
                if helper(word[1:], tree[word[0]]):
                    return True
            return False
        
        return helper(word, self.root)

421 数组中两个数的最大异或值

https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/

更多技术文章请点击查看

你可能感兴趣的:(leetcode刷题,数据结构,python)