[LeetCode] Shortest Word Distance 最短单词距离

 

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

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].java

Given word1 = “coding”word2 = “practice”, return 3.
Given word1 = "makes"word2 = "coding", return 1.数组

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.post

 

这道题让咱们给了咱们一个单词数组,又给定了两个单词,让咱们求这两个单词之间的最小距离,限定了两个单词不一样,并且都在数组中。我最早想到的方法比较笨,我首先想的是要用哈希表来作,创建每一个单词和其全部出现位置数组的映射,可是后来想一想,反正创建映射也要遍历一遍数组,咱们还不如直接遍历一遍数组,直接把两个给定单词全部出现的位置分别存到两个数组里,而后咱们在对两个数组进行两两比较更新结果,参见代码以下:url

 

解法一:spa

class Solution {
public:
    int shortestDistance(vector<string>& words, string word1, string word2) {
        vector<int> idx1, idx2;
        int res = INT_MAX;
        for (int i = 0; i < words.size(); ++i) {
            if (words[i] == word1) idx1.push_back(i);
            else if (words[i] == word2) idx2.push_back(i);
        }
        for (int i = 0; i < idx1.size(); ++i) {
            for (int j = 0; j < idx2.size(); ++j) {
                res = min(res, abs(idx1[i] - idx2[j]));
            }
        }
        return res;
    }
};

 

上面的那种方法并不高效,咱们其实须要遍历一次数组就能够了,咱们用两个变量p1,p2初始化为-1,而后咱们遍历数组,遇到单词1,就将其位置存在p1里,若遇到单词2,就将其位置存在p2里,若是此时p1, p2都不为-1了,那么咱们更新结果,参见代码以下:code

 

解法二:htm

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

 

下面这种方法只用一个辅助变量idx,初始化为-1,而后遍历数组,若是遇到等于两个单词中的任意一个的单词,咱们在看idx是否为-1,若不为-1,且指向的单词和当前遍历到的单词不一样,咱们更新结果,参见代码以下:blog

 

解法三:leetcode

class Solution {
public:
    int shortestDistance(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 && words[idx] != words[i]) {
                    res = min(res, i - idx);
                }
                idx = i;
            }
        }
        return res;
    }
};

 

相似题目:

Shortest Word Distance II

Shortest Word Distance III 

 

参考资料:

https://leetcode.com/discuss/50234/ac-java-clean-solution

https://leetcode.com/discuss/61820/java-only-need-to-keep-one-index

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索