[LeetCode] 245. Shortest Word Distance III 最短單詞距離之三


 

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.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “makes”, word2 = “coding”
Output: 1
Input: word1 = "makes", word2 = "makes"
Output: 3

Note:
You may assume word1 and word2 are both in the list.

 

這道題還是讓我們求最短單詞距離,有了之前兩道題 Shortest Word Distance II 和 Shortest Word Distance 的基礎,就大大降低了題目本身的難度。這里增加了一個條件,就是說兩個單詞可能會相同,所以在第一題中的解法的基礎上做一些修改,博主最先想的解法是基於第一題中的解法二,由於會有相同的單詞的情況,那么 p1 和 p2 就會相同,這樣結果就會變成0,顯然不對,所以要對 word1 和 word2 是否的相等的情況分開處理,如果相等了,由於 p1 和 p2 會相同,所以需要一個變量t來記錄上一個位置,這樣如果t不為 -1,且和當前的 p1 不同,可以更新結果,如果 word1 和 word2 不等,那么還是按原來的方法做,參見代碼如下:

 

解法一:

class Solution {
public:
    int shortestWordDistance(vector<string>& words, string word1, string word2) {
        int p1 = -1, p2 = -1, res = INT_MAX;
        for (int i = 0; i < words.size(); ++i) {
            int t = p1;
            if (words[i] == word1) p1 = i;
            if (words[i] == word2) p2 = i;
            if (p1 != -1 && p2 != -1) {
                if (word1 == word2 && t != -1 && t != p1) {
                    res = min(res, abs(t - p1));
                } else if (p1 != p2) {
                    res = min(res, abs(p1 - p2));
                }
            }
        }
        return res;
    }
};

 

上述代碼其實可以優化一下,我們並不需要變量t來記錄上一個位置,將 p1 初始化為數組長度,p2 初始化為數組長度的相反數,然后當 word1 和 word2 相等的情況,用 p1 來保存 p2 的結果,p2 賦為當前的位置i,這樣就可以更新結果了,如果 word1 和 word2 不相等,則還跟原來的做法一樣,這種思路真是挺巧妙的,參見代碼如下:

 

解法二:

class Solution {
public:
    int shortestWordDistance(vector<string>& words, string word1, string word2) {
        int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
        for (int i = 0; i < words.size(); ++i) {
            if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
            if (words[i] == word2) p2 = i;
            res = min(res, abs(p1 - p2));
        }
        return res;
    }
};

 

我們再來看一種更進一步優化的方法,只用一個變量 idx,這個 idx 的作用就相當於記錄上一次的位置,當前 idx 不等 -1 時,說明當前i和 idx 不同,然后在 word1 和 word2 相同或者 words[i] 和 words[idx] 相同的情況下更新結果,最后別忘了將 idx 賦為i,參見代碼如下;

 

解法三:

class Solution {
public:
    int shortestWordDistance(vector<string>& words, string word1, string word2) {
        int idx = -1, res = INT_MAX;
        for (int i = 0; i < words.size(); ++i) {
            if (words[i] == word1 || words[i] == word2) {
                if (idx != -1 && (word1 == word2 || words[i] != words[idx])) {
                    res = min(res, i - idx);
                }
                idx = i;
            }
        }
        return res;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/245

 

類似題目:

Shortest Word Distance II

Shortest Word Distance

 

參考資料:

https://leetcode.com/problems/shortest-word-distance-iii/

https://leetcode.com/problems/shortest-word-distance-iii/discuss/67097/12-16-lines-Java-C%2B%2B

https://leetcode.com/problems/shortest-word-distance-iii/discuss/67095/Short-Java-solution-10-lines-O(n)-modified-from-Shortest-Word-Distance-I

 

LeetCode All in One 題目講解匯總(持續更新中...)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM