243 & 244. Shortest Word Distanc

2018-04-19  本文已影响0人  Super_Alan

243: https://leetcode.com/problems/shortest-word-distance/description/

Given a list of words and two words word1 and word2, 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.

双指针

    public int shortestDistance(String[] words, String word1, String word2) {
        int distance = Integer.MAX_VALUE;
        int index1 = -1;
        int index2 = -1;
        
        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word1)) {
                index1 = i;
                if (index2 >= 0) {
                    distance = Math.min(distance, index1 - index2);
                }
            }
            
            if (words[i].equals(word2)) {
                index2 = i;
                if (index1 >= 0) {
                    distance = Math.min(distance, index2 - index1);
                }
            }
        }
        
        return distance;
    }

  1. https://leetcode.com/problems/shortest-word-distance-ii/description/

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.

使用 HashMap 存储 word 对应的 indexes。similar to merge two sorted list.

class WordDistance {
    // word => List of indexes in array words
    HashMap<String, List<Integer>> map = null;
    
    public WordDistance(String[] words) {
        initializeMap(words);
    }

    public int shortest(String word1, String word2) {
        List<Integer> positions1 = map.get(word1);
        List<Integer> positions2 = map.get(word2);
        
        int index1 = 0;
        int index2 = 0;
        int shortestDistance = Integer.MAX_VALUE;
        
        while (index1 < positions1.size() && index2 < positions2.size()) {
            int p1 = positions1.get(index1);
            int p2 = positions2.get(index2);
            
            if (p1 < p2) {
                shortestDistance = Math.min(shortestDistance, p2 - p1);
                index1++;
            } else {
                shortestDistance = Math.min(shortestDistance, p1 - p2);
                index2++;
            }
        }
        
        return shortestDistance;
    }
    
            
    private void initializeMap(String[] words) {
        map = new HashMap<String, List<Integer>>();
        
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (map.containsKey(word)) {
                map.get(word).add(i);
            } else {
                List<Integer> positions = new ArrayList<>();
                positions.add(i);
                map.put(word, positions);
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读