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

        共 3825字,需瀏覽 8分鐘

         ·

        2021-04-25 14:04

        算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(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.
        請設計一個類,使該類的構造函數能夠接收一個單詞列表。然后再實現一個方法,該方法能夠分別接收兩個單詞 word1 和 word2,并返回列表中這兩個單詞之間的最短距離。您的方法將被以不同的參數調用 多次。

        示例

        示例:
        假設 words = ["practice", "makes", "perfect", "coding", "makes"]

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


        解題


        因為會多次調用,我們不能每次調用的時候再把這兩個單詞的下標找出來。我們可以用一個哈希表,在傳入字符串數組時,就把每個單詞的下標找出存入表中。這樣當調用最短距離的方法時,我們只要遍歷兩個單詞的下標列表就行了。具體的比較方法,則類似merge two list,每次比較兩個list最小的兩個值,得到一個差值。然后把較小的那個給去掉。因為我們遍歷輸入數組時是從前往后的,所以下標列表也是有序的。

        public class WordDistance {
            
            HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
            
            public WordDistance(String[] words) {
                // 統(tǒng)計每個單詞出現的下標存入哈希表中
                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;
                // 每次比較兩個下標列表最小的下標,然后把跳過較小的那個
                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;
            }
        }


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

        上期推文:

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

        瀏覽 56
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

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

        手機掃一掃分享

        分享
        舉報
        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>
            韩国三级在线视频 | 色噜噜色综合 | 国产手机精品视频 | 成人免费看片 视频在线观看 | 国产一级爽片 | 成人黄片18 | 大屌操骚逼 | 永久AV免费网站 | 国内自拍视频网址 | 扒开伸进免费视频 |