美文网首页
Shortest Word Distance

Shortest Word Distance

作者: yanyuchen | 来源:发表于2017-11-25 07:06 被阅读0次

class Solution {

public int shortestDistance(String[] words, String word1, String word2) {

if(words == null || words.length == 0){

return -1;

}

int diff = Integer.MAX_VALUE;

int index = -1;

for(int i = 0; i < words.length; i++){

if( words[i].equals(word1) || words[i].equals(word2)){

if(index != -1 && !words[index].equals(words[i])){

diff = Math.min(i - index, diff);

}

index = i;

}

}

return diff;

}

}

相关文章

网友评论

      本文标题:Shortest Word Distance

      本文链接:https://www.haomeiwen.com/subject/fvmqbxtx.html