Easy
跟Shortest Word Distance iii差不多,更简单少了处理word1, word2相同那部分.
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int index1 = words.length;
int index2 = -words.length;
int shortest = 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;
}
shortest = Math.min(shortest, Math.abs(index1 - index2));
}
return shortest;
}
}