<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)244:最短單詞距離 II

          共 3825字,需瀏覽 8分鐘

           ·

          2021-04-25 14:04

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

          今天和大家聊的問題叫做 最短單詞距離II,我們先來看題面:
          https://leetcode-cn.com/problems/shortest-word-distance-ii/

          his is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
          Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
          請(qǐng)?jiān)O(shè)計(jì)一個(gè)類,使該類的構(gòu)造函數(shù)能夠接收一個(gè)單詞列表。然后再實(shí)現(xiàn)一個(gè)方法,該方法能夠分別接收兩個(gè)單詞 word1 和 word2,并返回列表中這兩個(gè)單詞之間的最短距離。您的方法將被以不同的參數(shù)調(diào)用 多次。

          示例

          示例:
          假設(shè) words = ["practice", "makes", "perfect", "coding", "makes"]

          輸入: word1 = “coding”, word2 = “practice”
          輸出: 3
          輸入: word1 = "makes", word2 = "coding"
          輸出: 1


          解題


          因?yàn)闀?huì)多次調(diào)用,我們不能每次調(diào)用的時(shí)候再把這兩個(gè)單詞的下標(biāo)找出來。我們可以用一個(gè)哈希表,在傳入字符串?dāng)?shù)組時(shí),就把每個(gè)單詞的下標(biāo)找出存入表中。這樣當(dāng)調(diào)用最短距離的方法時(shí),我們只要遍歷兩個(gè)單詞的下標(biāo)列表就行了。具體的比較方法,則類似merge two list,每次比較兩個(gè)list最小的兩個(gè)值,得到一個(gè)差值。然后把較小的那個(gè)給去掉。因?yàn)槲覀儽闅v輸入數(shù)組時(shí)是從前往后的,所以下標(biāo)列表也是有序的。

          public class WordDistance {
              
              HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
              
              public WordDistance(String[] words) {
                  // 統(tǒng)計(jì)每個(gè)單詞出現(xiàn)的下標(biāo)存入哈希表中
                  for(int i = 0; i < words.length; i++){
                      List<Integer> cnt = map.get(words[i]);
                      if(cnt == null){
                          cnt = new ArrayList<Integer>();
                      }
                      cnt.add(i);
                      map.put(words[i], cnt);
                  }
              }

              public int shortest(String word1, String word2) {
                  List<Integer> idx1 = map.get(word1);
                  List<Integer> idx2 = map.get(word2);
                  int distance = Integer.MAX_VALUE;
                  int i = 0, j = 0;
                  // 每次比較兩個(gè)下標(biāo)列表最小的下標(biāo),然后把跳過較小的那個(gè)
                  while(i < idx1.size() && j < idx2.size()){
                      distance = Math.min(Math.abs(idx1.get(i) - idx2.get(j)), distance);
                      if(idx1.get(i) < idx2.get(j)){
                          i++;
                      } else {
                          j++;
                      }
                  }
                  return distance;
              }
          }


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

          上期推文:

          LeetCode1-240題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)241:為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級(jí)
          LeetCode刷題實(shí)戰(zhàn)242:有效的字母異位詞
          LeetCode刷題實(shí)戰(zhàn)243:最短單詞距離

          瀏覽 56
          點(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>
                  色色色色色色91 | 久久三级片网站 | 91aaa精品无码 | 操屄免费视频 | 色五月播五月丁香综合 |