成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

leetcode-211-Add and Search Word - Data structure

Alliot / 2637人閱讀

摘要:原題總結(jié)棧的利用,先進(jìn)后出的作用,可以保持鏈表一類的數(shù)據(jù)的連貫操作,可以用來(lái)替代廣度搜索。每一層次可以用進(jìn)棧出棧進(jìn)行替代。形式的數(shù)據(jù)結(jié)構(gòu),有記憶狀態(tài)的作用。應(yīng)用字符串的遍歷,廣度搜索。

原題:
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.

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.
總結(jié):棧 stack 的利用,先進(jìn)后出的作用,可以保持 鏈表一類的數(shù)據(jù)的連貫操作,可以用來(lái)替代廣度搜索。
每一層次可以用進(jìn)棧出棧進(jìn)行替代。
stack=[(node,str)],形式的數(shù)據(jù)結(jié)構(gòu),有記憶狀態(tài)的作用。
應(yīng)用: 字符串的遍歷,廣度搜索。
    
import collections

class WordNode:
    def __init__(self):
        self.children = {}
        self.isEnd = False

class WordDictionary:
    def __init__(self):
        self.root = WordNode()

    def addWord(self, word):
        cur_node=self.root
        for char_iter in word:
            if char_iter in cur_node.children:
                cur_node=cur_node.children[char_iter]
            else:
                new_node=WordNode()
                cur_node.children[char_iter]=new_node
                cur_node=new_node
            # cur_node=cur_node.children[char_iter]
        cur_node.isEnd=True

    def search(self, word):
        stack=[(self.root,word)]
        while stack:
            node,w=stack.pop()
            if not w:
                if node.isEnd:
                    return True
            # print([w])
            elif w[0] == ".":
                for node_iter in node.children.values():
                    stack.append((node_iter,w[1:]))
            elif w[0] in node.children:
                nodes_cur=node.children[w[0]]
                stack.append((nodes_cur,w[1:]))
            else:
                pass
        return False




class WordDictionary:
    def __init__(self):
        self.root = WordNode()

    def addWord(self, word):
        node = self.root
        for w in word:
            if w in node.children:
                node = node.children[w]
            else:
                node.children[w] = WordNode()
                node = node.children[w]
        node.isEnd = True

    def search(self, word):
        stack = [(self.root, word)]
        while stack:
            node, w = stack.pop()
            if not w:
                if node.isEnd:
                    return True
            elif w[0] == ".":
                for n in node.children.values():
                    stack.append((n, w[1:]))
            else:
                if w[0] in node.children:
                    n = node.children[w[0]]
                    stack.append((n, w[1:]))
        return False


if __name__=="__main__":
    wd=WordDictionary()
    # wd.addWord("bad")
    # wd.addWord("dad")
    # wd.addWord("mad")
    # out=wd.search("pad")
    # print(out)
    # out =wd.search("bad")
    # print(out)
    # out =wd.search(".ad")
    # print(out)
    # out =wd.search("b..")
    # print(out)

    words=["WordDictionary", "addWord", "addWord", "search", "search", "search", "search", "search", "search"]
    detect_words=[ ["a"], ["a"], ["."], ["a"], ["aa"], ["a"], [".a"], ["a."]]
    for word in words:
        wd.addWord(word)
    for detect_word in detect_words:
        out=wd.search(detect_word[0])
        print(out)

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/42286.html

相關(guān)文章

  • [LeetCode] 211. Add and Search Word - Data structu

    Problem 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 let...

    mozillazg 評(píng)論0 收藏0
  • [Leetcode] Implement Trie 實(shí)現(xiàn)前綴樹

    摘要:壓縮前綴樹其實(shí)就是將所有只有一個(gè)子節(jié)點(diǎn)的節(jié)點(diǎn)合并成一個(gè),以減少?zèng)]有意義的類似鏈表式的鏈接。然后我們開始遍歷這個(gè)前綴樹。 Implement Trie Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowe...

    jsliang 評(píng)論0 收藏0
  • [LintCode] Implement Trie

    Problem Implement a trie with insert, search, and startsWith methods. Example insert(lintcode) search(code) // return false startsWith(lint) // return true startsWith(linterror) // return false insert...

    chengjianhua 評(píng)論0 收藏0
  • [LintCode/LeetCode] Implement Trie

    摘要:首先,我們應(yīng)該了解字典樹的性質(zhì)和結(jié)構(gòu),就會(huì)很容易實(shí)現(xiàn)要求的三個(gè)相似的功能插入,查找,前綴查找。既然叫做字典樹,它一定具有順序存放個(gè)字母的性質(zhì)。所以,在字典樹的里面,添加,和三個(gè)參數(shù)。 Problem Implement a trie with insert, search, and startsWith methods. Notice You may assume that all i...

    付永剛 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<