### 题目

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.

### 一次遍历

• time: O(n)
• space: O(1)

#### Java代码

public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int mindis = -1;
int pos1 = -1, pos2 = -1;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) { // do not use "==" to compare string
pos1 = i;
if (pos2 != -1) {
int dis = pos1 - pos2;
mindis = (mindis == -1)? dis : Math.min(mindis,dis);
}
}
if (words[i].equals(word2)) { // do not use "==" to compare string
pos2 = i;
if (pos1 != -1) {
int dis = pos2 - pos1;
mindis = (mindis == -1)? dis : Math.min(mindis,dis);
}
}
}
return (mindis == -1)? 0 : mindis;
}
}


#### 结果 #### Java代码（简洁版）

public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int mindis = -1;
int pos1 = -1, pos2 = -1;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) { pos1 = i; }
if (words[i].equals(word2)) { pos2 = i; }
if (pos1 != -1 && pos2 != -1) {
int dis = Math.abs(pos1 - pos2);
mindis = (mindis == -1)? dis : Math.min(mindis,dis);
}
}
return (mindis == -1)? 0 : mindis;
}
}


#### 结果 