zoukankan      html  css  js  c++  java
  • Trie树

    Trie树:字典查找树。

    208. Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods.

    Note:
    You may assume that all inputs are consist of lowercase letters a-z.

    Solution

    class TrieNode(object):
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.word = False
            self.children = {}
    
    class Trie(object):
    
        def __init__(self):
            self.root = TrieNode()
    
        def insert(self, word):
            """
            Inserts a word into the trie.
            :type word: str
            :rtype: void
            """
            node = self.root
            for i in word:
                if i not in node.children:
                    node.children[i] = TrieNode()
                node = node.children[i]
            node.word = True
    
        def search(self, word):
            """
            Returns if the word is in the trie.
            :type word: str
            :rtype: bool
            """
            node = self.root
            for i in word:
                if i not in node.children:
                    return False
                node = node.children[i]
            return node.word
    
        def startsWith(self, prefix):
            """
            Returns if there is any word in the trie
            that starts with the given prefix.
            :type prefix: str
            :rtype: bool
            """
            node = self.root
            for i in prefix:
                if i not in node.children:
                    return False
                node = node.children[i]
            return True
    
    # Your Trie object will be instantiated and called as such:
    # trie = Trie()
    # trie.insert("somestring")
    # trie.search("key")
    

    211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations:

    void addWord(word)
    bool search(word)
    

    search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

    For example:

    addWord("bad")
    addWord("dad")
    addWord("mad")
    search("pad") -> false
    search("bad") -> true
    search(".ad") -> true
    search("b..") -> true
    

    Note:
    You may assume that all words are consist of lowercase letters a-z.

    Solution

    class TrieNode():
        def __init__(self):
            self.isWord = False
            self.children = {}
    
    class WordDictionary(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.root = TrieNode()
    
        def addWord(self, word):
            """
            Adds a word into the data structure.
            :type word: str
            :rtype: void
            """
            node = self.root
            for i in word:
                if i not in node.children:
                    node.children[i] = TrieNode()
                node = node.children[i]
            node.isWord = True
    
        def search(self, word):
            """
            Returns if the word is in the data structure. A word could
            contain the dot character '.' to represent any one letter.
            :type word: str
            :rtype: bool
            """
            return self.helper(word, self.root)
            
        def helper(self, word, node):
            if len(word) == 0:
                return node.isWord
            first = word[0]
            if first == ".":
                for key in node.children:
                    # return true if any was true
                    if self.helper(word[1:], node.children[key]):
                        return True
            else:
                if first not in node.children:
                    return False
                return self.helper(word[1:], node.children[first])
            return False
                
            
    
    # Your WordDictionary object will be instantiated and called as such:
    # wordDictionary = WordDictionary()
    # wordDictionary.addWord("word")
    # wordDictionary.search("pattern")
    
  • 相关阅读:
    毕业设计论文撰写指南(10)—— 总体要求
    毕业设计论文撰写指南(09)—— 致谢+参考文献
    毕业设计论文撰写指南(08)—— 第六章 结论
    毕业设计论文撰写指南(07)—— 第五章 ***系统详细设计与实现
    Interlocked.CompareExchange
    Memcached、Redis和MongoDB的区别
    relatedTarget、fromElement、toElement之间的关系
    异常:System.BadImageFormatException,未能加载正确的程序集XXX
    Windows Service
    启用 CORS 来解决这个问题(ajax跨域请求)
  • 原文地址:https://www.cnblogs.com/binwone/p/6230819.html
Copyright © 2011-2022 走看看