<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 8768字,需瀏覽 18分鐘

           ·

          2021-03-14 14:04

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

          今天和大家聊的問題叫做 實(shí)現(xiàn) Trie (前綴樹),我們先來看題面:
          https://leetcode-cn.com/problems/implement-trie-prefix-tree/

          Trie (we pronounce "try") or prefix tree is a tree data structure used to retrieve a key in a strings dataset. There are various applications of this very efficient data structure, such as autocomplete and spellchecker.

          題意

          實(shí)現(xiàn)一個(gè) Trie (前綴樹),包含 insert, search, 和 startsWith 這三個(gè)操作。

          示例


          Trie trie = new Trie();

          trie.insert("apple");
          trie.search("apple"); // 返回 true
          trie.search("app"); // 返回 false
          trie.startsWith("app"); // 返回 true
          trie.insert("app");
          trie.search("app"); // 返回 true

          說明:

          你可以假設(shè)所有的輸入都是由小寫字母 a-z 構(gòu)成的。
          保證所有輸入均為非空字符串。


          解題

          https://segmentfault.com/a/1190000015804960

          思路分析:


          樹是由節(jié)點(diǎn)組成,節(jié)點(diǎn)定義應(yīng)該包含節(jié)點(diǎn)值(前綴樹的定義,值應(yīng)該為一個(gè)字符char)和葉子節(jié)點(diǎn)的指針,但是為了識(shí)別是否為一個(gè)單詞的最后一個(gè)字符,所以增加一個(gè)boolean變量識(shí)別。

          由于已知輸入為全小寫(a-z)的字母,所以可以使用一個(gè)長度為26的數(shù)組存儲(chǔ)葉子節(jié)點(diǎn)。且由于a-z的ASCII碼是連續(xù)的,其ASCII是從97-123,所以可以直接使用 ASCII碼-97=對(duì)應(yīng)節(jié)點(diǎn)的數(shù)組下標(biāo)。


          class Trie {
              /**
               * 當(dāng)前節(jié)點(diǎn)的值
               */

              public char value;
              /**
               * a-z有26個(gè)字母,需要訪問時(shí)由于a的ASCII碼為97,所以所有字母訪問的對(duì)應(yīng)下表皆為 字母的ASCII碼-97
               */

              public Trie[] children=new Trie[26];
              /**
               * 標(biāo)識(shí)此節(jié)點(diǎn)是否為某個(gè)單詞的結(jié)束節(jié)點(diǎn)
               */

              public boolean endAsWord=false;
              
              public Trie() {
                  
              }
              /**
               * 插入一個(gè)單詞
               * @param word 單詞
               */

              public void insert(String word) {
                  if(word!=null){
                      //分解成字符數(shù)組
                      char[] charArr=word.toCharArray();
                      //模擬指針操作,記錄當(dāng)前訪問到的樹的節(jié)點(diǎn)
                      Trie currentNode=this;
                      for(int i=0;i<charArr.length;i++){
                          char currentChar=charArr[i];
                          //根據(jù)字符獲取對(duì)應(yīng)的子節(jié)點(diǎn)
                          Trie node=currentNode.children[currentChar-97];
                          if(node!=null && node.value==currentChar){//判斷節(jié)點(diǎn)是否存在
                             currentNode=node;
                          }else{//不存在則創(chuàng)建一個(gè)新的葉子節(jié)點(diǎn),并指向當(dāng)前的葉子節(jié)點(diǎn)
                              node=new Trie();
                              node.value=currentChar;
                              currentNode.children[currentChar-97]=node;
                              currentNode=node;
                          }
                      }
                      //這個(gè)標(biāo)識(shí)很重要
                      currentNode.endAsWord=true;
                  }
              }
              /**
               * 檢索指定單詞是否在樹中
               * @param word 單詞
               */

              public boolean search(String word) {
                  boolean result=true;
                  if(word!=null && !word.trim().equals("")){
                      char[] prefixChar=word.toCharArray();
                      Trie currentNode=this;
                      for(int i=0;i<prefixChar.length;i++){
                          char currentChar=prefixChar[i];
                          Trie node=currentNode.children[currentChar-97];
                          if(node!=null && node.value==currentChar){//判斷節(jié)點(diǎn)是否存在
                             currentNode=node;
                          }else{
                              result=false;
                              break;
                          }
                      }
                      if(result){
                          result=currentNode.endAsWord;
                      }
                  }
                  return result;
              }
              /**
               * 檢索指定前綴是否在樹中
               * @param word 單詞
               */

              public boolean startsWith(String prefix) {
                  boolean result=true;
                  if(prefix!=null && !prefix.trim().equals("")){
                      char[] prefixChar=prefix.toCharArray();
                      Trie currentNode=this;
                      for(int i=0;i<prefixChar.length;i++){
                          char currentChar=prefixChar[i];
                          Trie node=currentNode.children[currentChar-97];
                          if(node!=null && node.value==currentChar){//判斷節(jié)點(diǎn)是否存在
                              currentNode=node;
                          }else{
                              result=false;
                              break;
                          }
                      }
                  }
                  return result;
              }
          }


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

          上期推文:

          LeetCode1-200題匯總,希望對(duì)你有點(diǎn)幫助!

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

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

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

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

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

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

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


          瀏覽 48
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人性爱福利视频在线 | 正在播放国产AV | 成人毛片女人免费看 | 嫩逼逼网| 麻豆福利导航 |