题目描述:
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
Example :
Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].
Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = “makes”, word2 = “coding”
Output: 1
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Time complexity: O(nm)
用两个指针index1和index2分别记录word1和word2最近出现的位置,每次找到新的单词是二者中的一个,则计算差值并与之前的差值比较取最小值。
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int index1 = -1;
int index2 = -1;
int minDistance = words.length;
for(int i = 0; i < words.length; i++){
if(words[i].equals(word1)){
index1 = i;
}else if(words[i].equals(word2)){
index2 = i;
}
if(index1 != -1 && index2 != -1){
minDistance = Math.min(minDistance, Math.abs(index1-index2));
}
}
return minDistance;
}
}