leetcode243--Shortest Word Distance

# 小郑之家~

### 解决办法

• 最容易想到的是，遍历一次数组，用两个新的数组记录这两个单词出现的位置，然后再找最小的距离 ，
class Solution:
public:
int shortestDistance(vector<string>& words, string word1, string word2){

vector<int> idx1, idx2;   //用来存他们的id的
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;
};



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;

}
};



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[i] != words[idx]) res=min(res, abs(idx-i));
idx = i;
}
}

}
}