Shortest Word Distance II

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and 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.

 follow up: Shortest Word Distance II,需要处理word1和word2相同的情况,并需要注意该情况下list中只有一个word1的case

 follow up 是问 shortest() 每次给三个词,而不是两个词,求三个词里最左边的到最右边的词的距离的最小值。follow up 就是3个指针呗……

public int shortest(String word1, String word2, String word3) {
    List<Integer> list1 = map.get(word1);
    List<Integer> list2 = map.get(word2);
    List<Integer> list3 = map.get(word3);
    int ret = Integer.MAX_VALUE;. 1point 3acres 璁哄潧
    for (int i = 0, j = 0, k = 0; i < list1.size() && j < list2.size() && k < list3.size(); ) {
        int index1 = list1.get(i), index2 = list2.get(j), index3 = list3.get(k);
        if (index1 < index2 && index1 < index3) {.1point3acres缃�
            ret = Math.min(ret, Math.max(index2, index3) - index1);. 1point 3acres 璁哄潧
            i++;
        } else if (index2 < index1 && index2 < index3) {. From 1point 3acres bbs
            ret = Math.min(ret, Math.max(index1, index3) - index2);
            j++;
        } else {
            ret = Math.min(ret, Math.max(index1, index2) - index3);
            k++;
        }
    }
    return ret;
}

  

哈希表法

复杂度

时间 O(N) 空间 O(N)

思路

因为会多次调用,我们不能每次调用的时候再把这两个单词的下标找出来。我们可以用一个哈希表,在传入字符串数组时,使用HashMap来储存word以及word在Array里面出现的index。这样当调用最短距离的方法时,我们只要遍历两个单词的下标列表就行了。具体的比较方法,则类似merge two list,每次比较两个list最小的两个值,得到一个差值。然后把较小的那个给去掉。因为我们遍历输入数组时是从前往后的,所以下标列表也是有序的。

public class WordDistance {
    
    HashMap<String, ArrayList<Integer>> map;

    public WordDistance(String[] words) {
        this.map = new HashMap<String, ArrayList<Integer>>();
        for (int i=0; i<words.length; i++) {
            String item = words[i];
            if (map.containsKey(item)) {
                map.get(item).add(i);
            }
            else {
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(i);
                map.put(item, list);
            }
        }
    }

    public int shortest(String word1, String word2) {
        ArrayList<Integer> l1 = map.get(word1);
        ArrayList<Integer> l2 = map.get(word2);
        int minDis = Integer.MAX_VALUE;
        int i=0, j=0;
        while (i<l1.size() && j<l2.size()) {
            int p1 = l1.get(i);
            int p2 = l2.get(j);
            minDis = Math.min(minDis, Math.abs(p1-p2));
            if (p1 < p2) i++;
            else j++;
        }
        return minDis;
    }
}

// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");

  

posted @ 2017-11-07 04:33  apanda009  阅读(132)  评论(0编辑  收藏  举报