- 243 & 244. Shortest Word Distanc
- 243 & 245. Shortest Word Distanc
- [LeetCode 243/244/245] Shortest
- 244. Shortest Word Distance II
- 244. Shortest Word Distance II
- 244. Shortest Word Distance II (
- 244. Shortest Word Distance II
- 243. Shortest Word Distance
- 243. Shortest Word Distance
- 244. Shortest Word Distance II M
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;
}
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);
}
}
}
}
网友评论