• 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")
    
  • 相关阅读:
    FineUI开源版(ASP.Net)初学手册-部分JS整理
    ASP.NET-FineUI开发实践-15
    ASP.NET-FineUI开发实践-14
    FineUI开源版(ASP.Net)开发实践-目录
    ASP.NET-FineUI开发实践-13(二)
    ASP.NET-FineUI开发实践-13(一)
    在VS.NET中根据条件设置不同的MainForm
    VB.NET在基类中定义共享事件(类似于C#中的静态事件)
    也谈解决Combobox绑定数据后取值出现System.Data.DataRowView的问题
    在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库
  • 原文地址:https://www.cnblogs.com/binwone/p/6230819.html
Copyright © 2020-2023  润新知