245. Shortest Word Distance III

Medium

自己写出来的,没什么难度

class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
        HashMap> hash = new HashMap<>();
        int index = 0;
        for (String word : words){
            if (!hash.containsKey(word)){
                hash.put(word, new ArrayList<>(Arrays.asList(index++)));
            } else {
                hash.get(word).add(index++);
            }
        }
        int distance = 0;
        int shortest = words.length;
        if (word1.equals(word2)){
            int i = 0;
            int j = 1;
            while (j < hash.get(word1).size()){
                distance = hash.get(word1).get(j) - hash.get(word1).get(i);
                if (distance < shortest){
                    shortest = distance;
                }
                i++;
                j++;
            }
            return shortest;
        }
        List indexes1 = hash.get(word1);
        List indexes2 = hash.get(word2);
        int i = 0;
        int j = 0;
        boolean isSame = false;
        while (i < indexes1.size() && j < indexes2.size()){
            int index1 = indexes1.get(i);
            int index2 = indexes2.get(j);
            if (index1 < index2){
                distance = index2 - index1;
                i++;
            } else {
                distance = index1 - index2;
                j++;
            }
            if (distance < shortest){
                shortest = distance;
            } 
        }
        return shortest;
    }
}

然而这个code太不concise了,尤其是对word1.equals(word2)的处理,下面是改进版本.
注意几点:

  • i1,i2不能初始化为0,而且你去试一试初始化为:
    int i1 = Integer.MAX_VALUE, int i2 = -Integer.MAX_VALUE也会出问题(下面细谈)
  • if (word1.equals(word2)){i1 = i2};的意思就是如果word1跟word2相同,我们就更新i2到当前i, i1恢复到之前i2的地方,就是上一个找到word[i]跟word1,word2相同的i. 这样每次求的都是最近两个index的差,以此来找到最短.
class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
        int i1 = words.length;
        int i2 = -words.length;
        int shortest = words.length;
        for (int i = 0; i < words.length; i++){
            if (words[i].equals(word1)){
                i1 = i;
            }
            if (words[i].equals(word2)){
                if (word1.equals(word2)){
                    i1 = i2;
                }
                i2 = i;        
            }
            shortest = Math.min(shortest, Math.abs(i1 - i2));
        }
        return shortest;
    }
}

关于为什么初始化为int i1 =0, i2 = 0不行,这个很好理解吧.因为这样会二话不说导致shortest = 0;那么为什么不能初始化为int i1 = Integer.MAX_VALUE, int i2 = Integer.MAX_VALUE呢?
因为在这行代码中:
shortest = Math.min(shortest, Math.abs(i1 - i2));
i1 - i2即1 + Integer.MAX_VALUE = Integer.MIN_VALUE
(int范围是:-2147483648~2147483647)
Math.abs(Integer.MIN_VALUE) = Integer.MIN_VALUE;
所以会得到一个为负数的绝对值,这些都是规定的,原因暂不讨论.
所以可以用下面cast的方法防止溢出的问题:

class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
        long i1 = Integer.MAX_VALUE;
        long i2 = -Integer.MAX_VALUE;
        long shortest = words.length;//4
        for (int i = 0; i < words.length; i++){
            if (words[i].equals(word1)){
                i1 = i;
            }
            if (words[i].equals(word2)){
                if (word1.equals(word2)){
                    i1 = i2;
                }
                i2 = i;        
            }
            shortest = Math.min(shortest, Math.abs(i1 - i2));
        }
        return (int) shortest;
    }
}

你可能感兴趣的:(245. Shortest Word Distance III)