您的位置:首页 > 其它

[LeetCode]Shortest Word Distance

2015-11-27 16:14 295 查看
public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int pre1 = -1;
int pre2 = -1;
int result = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word.equals(word1)) {
if (pre2 != -1) {
result = Math.min(i - pre2, result);
}
pre1 = i;
} else if (word.equals(word2)) {
if (pre1 != -1) {
result = Math.min(i - pre1, result);
}
pre2 = i;
}
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: