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

          共 2754字,需瀏覽 6分鐘

           ·

          2021-04-25 14:02

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

          今天和大家聊的問題叫做 最短單詞距離III,我們先來看題面:
          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.

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

          示例


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

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

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

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


          解題


          這題和I是一樣的,唯一不同的是對于word1和word2相同的時候,我們要區(qū)分第一次遇到和第二次遇到這個詞。這里加入了一個turns,如果是相同單詞的話,每次遇到一個單詞turn加1,這樣可以根據(jù)turn來判斷是否要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;
              }
          }


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

          上期推文:

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

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  求A片网址 | 亚欧洲精品在线视频 | 五月天色婷婷中文影院 | 九七97在线 | 特级西西人体WWWww |