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刷題實(shí)戰(zhàn)245:最短單詞距離 III

        共 2754字,需瀏覽 6分鐘

         ·

        2021-04-25 14:02

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

        今天和大家聊的問(wèn)題叫做 最短單詞距離III,我們先來(lái)看題面:
        https://leetcode-cn.com/problems/shortest-word-distance-iii/

        This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

        Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

        word1 and word2 may be the same and they represent two individual words in the list.

        給定一個(gè)單詞列表和兩個(gè)單詞 word1 和 word2,返回列表中這兩個(gè)單詞之間的最短距離。
        word1 和 word2 是有可能相同的,并且它們將分別表示為列表中兩個(gè)獨(dú)立的單詞。

        示例


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

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

        輸入: word1 = "makes", word2 = "makes"
        輸出: 3

        注意:
        你可以假設(shè) word1 和 word2 都在列表里。


        解題


        這題和I是一樣的,唯一不同的是對(duì)于word1和word2相同的時(shí)候,我們要區(qū)分第一次遇到和第二次遇到這個(gè)詞。這里加入了一個(gè)turns,如果是相同單詞的話(huà),每次遇到一個(gè)單詞turn加1,這樣可以根據(jù)turn來(lái)判斷是否要switch。

        public class Solution {
            public int shortestWordDistance(String[] words, String word1, String word2) {
                int idx1 = -1, idx2 = -1, distance = Integer.MAX_VALUE, turn = 0, inc = (word1.equals(word2) ? 1 : 0);
                for(int i = 0; i < words.length; i++){
                    if(words[i].equals(word1) && turn % 2 == 0){
                        idx1 = i;
                        if(idx2 != -1) distance = Math.min(distance, idx1 - idx2);
                        turn += inc;
                    } else if(words[i].equals(word2)){
                        idx2 = i;
                        if(idx1 != -1) distance = Math.min(distance, idx2 - idx1);
                        turn += inc;
                    }
                }
                return distance;
            }
        }


        好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(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:最短單詞距離

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

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        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>
            亚洲成av | 卧底受呻吟两腿合不拢bl | 尤物视频yw.193.c㎝ | 免费古装一级淫片 | 亚洲国产18禁 | 成人做爱视频免费观看 | 久久春色 | 动漫美女被到爽巨乳同人 | 日韩成人电影在线 | 师尊双性精跪趴灌满h微博 |