[Locked] Shortest Word Distance I & II & III

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.

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

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.

代码:

int swd(vector<string> words, string word1, string word2) {
    int last = 0, pos1 = -1, pos2 = -1, mindist = INT_MAX;
    for(int i = 0; i < words.size(); i++) {
        if(words[i] == word1) {
            pos1 = i;
            if(last == 2)
                mindist = min(mindist, i - pos2);
            last = 1;
        }
        if(words[i] == word2) {
            pos2 = i;
            if(last == 1)
                mindist = min(mindist, i - pos1);
            last = 2;
        }
    }
    return mindist;
}
Shortest Word Distance II
This 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.

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

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.

代码:

class Solution {
private:
    unordered_multimap<string, int> hash;
public:
    Solution(vector<string> words) {
        for(int i = 0; i < words.size(); i++)
            hash.insert(make_pair(words[i], i));
    }
    int swd(string word1, string word2) {
        auto range1 = hash.equal_range(word1), range2 = hash.equal_range(word2);
        auto pos1 = range1.first, pos2 = range2.first;
        int mindist = INT_MAX;
        while(pos1 != range1.second && pos2 != range2.second){
            mindist = min(mindist, abs(pos1->second - pos2->second));
            pos1->second > pos2->second ? pos1++ : pos2++;
        }
        return mindist;
    }
};

 

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.

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

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

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

代码:

int swd(vector<string> words, string word1, string word2) {
    int last = 0, pos1 = -1, pos2 = -1, mindist = INT_MAX;
    if(word1 == word2) {
        for(int i = 0; i < words.size(); i++) {
            if(words[i] == word1) {
                if(pos1 != -1)
                    mindist = min(mindist, i - pos1);
                pos1 = i;
            }
        }
        return mindist;
    }
    for(int i = 0; i < words.size(); i++) {
        if(words[i] == word1) {
            pos1 = i;
            if(last == 2)
                mindist = min(mindist, i - pos2);
            last = 1;
        }
        if(words[i] == word2) {
            pos2 = i;
            if(last == 1)
                mindist = min(mindist, i - pos1);
            last = 2;
        }
    }
    return mindist;
}

 

原文地址:https://www.cnblogs.com/littletail/p/5200975.html