1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)211:添加與搜索單詞

        共 5051字,需瀏覽 11分鐘

         ·

        2021-03-17 14:01

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問題叫做 添加與搜索單詞,我們先來看題面:
        https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/

        Design a data structure that supports adding new words and finding if a string matches any previously added string.

        題意


        請你設(shè)計一個數(shù)據(jù)結(jié)構(gòu),支持 添加新單詞 和 查找字符串是否與任何先前添加的字符串匹配 。

        實現(xiàn)詞典類 WordDictionary :

        • WordDictionary() 初始化詞典對象

        • void addWord(word) 將 word 添加到數(shù)據(jù)結(jié)構(gòu)中,之后可以對它進行匹配

        • bool search(word) 如果數(shù)據(jù)結(jié)構(gòu)中存在字符串與 word 匹配,則返回 true ;否則,返回  false 。word 中可能包含一些 '.' ,每個 . 都可以表示任何一個字母。


        示例


        輸入:
        ["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
        [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
        輸出:
        [null,null,null,null,false,true,true,true]

        解釋:
        WordDictionary wordDictionary = new WordDictionary();
        wordDictionary.addWord("bad");
        wordDictionary.addWord("dad");
        wordDictionary.addWord("mad");
        wordDictionary.search("pad"); // return False
        wordDictionary.search("bad"); // return True
        wordDictionary.search(".ad"); // return True
        wordDictionary.search("b.."); // return True
         

        提示:
        1 <= word.length <= 500
        addWord 中的 word 由小寫英文字母組成
        search 中的 word 由 '.' 或小寫英文字母組成
        最多調(diào)用 50000 次 addWord 和 search


        解題


        用字典樹來作為存儲的數(shù)據(jù)結(jié)構(gòu)。
        新增單詞的時候,就使用字典樹插入新單詞的方法。
        在查找某一個字典樹時,使用深度優(yōu)先搜索即可。

        class WordDictionary {
        public:
            //定義字典樹中每個節(jié)點的結(jié)構(gòu)
            struct Node{
                bool isword = false; //用于標(biāo)記當(dāng)前節(jié)點是否為單詞的最后一個字符
                Node* next[27] = {};
            };
            Node* root; //每一個class都有一棵字典樹
            /** Initialize your data structure here. */
            WordDictionary() {
                root = new Node(); //新建一棵字典樹
            }
            
            /** Adds a word into the data structure. */
            void addWord(string word) {
                Node* tmp = root;
                for(auto w: word){
                    if(tmp->next[w - 'a'] == NULL){ //還沒有這個節(jié)點
                        Node* tt = new Node();
                        tmp->next[w - 'a'] = tt; //那就新建節(jié)點
                    }
                    tmp = tmp->next[w - 'a'];
                }
                tmp->isword = true; //遍歷完一個單詞
            }
            
            /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
            bool search(string word) {
                return dfs(word, root, 0);
            }
            
            bool dfs(string word, Node* root, int i){
                if(!root) return false;
                if(i >= word.size()) return root->isword; //看看是不是一個完整的單詞
                if(word[i] != '.'){
                    if(root->next[word[i] - 'a']){
                        return dfs(word, root->next[word[i] - 'a'], i+1);
                    }
                    else return false;
                }
                for(auto t: root->next){
                    if(t){
                        if(dfs(word, t, i+1)) return true;
                    }
                }
                return false;
            }
        };


        好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

        上期推文:

        LeetCode1-200題匯總,希望對你有點幫助!

        LeetCode刷題實戰(zhàn)201:數(shù)字范圍按位與

        LeetCode刷題實戰(zhàn)202:快樂數(shù)

        LeetCode刷題實戰(zhàn)203:移除鏈表元素

        LeetCode刷題實戰(zhàn)204:計數(shù)質(zhì)數(shù)

        LeetCode刷題實戰(zhàn)205:同構(gòu)字符串

        LeetCode刷題實戰(zhàn)206:反轉(zhuǎn)鏈表

        LeetCode刷題實戰(zhàn)207:課程表

        LeetCode刷題實戰(zhàn)208:實現(xiàn) Trie (前綴樹)

        LeetCode刷題實戰(zhàn)209:長度最小的子數(shù)組

        LeetCode刷題實戰(zhàn)210:課程表 II


        瀏覽 39
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            银行丝袜人妻第14部 | 中文字幕一区二区三区四区五区六区 | 国产日产欧产美韩**毛片 | 国产精品偷伦视频免费观看国产 | 人人人超碰97 | 国语偷拍 | 午夜天堂精品久久久久 | 久热免费 | 亚洲天堂精品视频 | 美女被爆插 |