243. Shortest Word Distance

没什么特别的

 1     public int shortestDistance(String[] words, String word1, String word2) {
 2         int idx1 = -1;
 3         int idx2 = -1;
 4         int min = Integer.MAX_VALUE;
 5         for(int i = 0; i < words.length; i++) {
 6             if(word1.equals(words[i])) {
 7                 idx1 = i;
 8             } else if(word2.equals(words[i])) {
 9                 idx2 = i;
10             }
11             if(idx1 != -1 && idx2 != -1) {
12                 min = Math.min(min, Math.abs(idx1-idx2));
13             }
14         }
15         return min;
16     }

 

posted @ 2016-07-30 06:15  warmland  阅读(108)  评论(0编辑  收藏  举报