Shortest Word Distance IIDot5
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 数据结构的使用:使用HashMap:ShortestWordDistance系列(也有可能电话面试题)
Page 1 of 1
Shortest Word Distance IIDot5
static HashMap<String, List<Integer>> wordIndex = new HashMap<>();
public static void WordDistance(String[] words) {
for (int i = 0; i < words.length; i++) {
if (!wordIndex.containsKey(words[i]))
wordIndex.put(words[i], new ArrayList<>());
wordIndex.get(words[i]).add(i);
}
}
public int minDistance(String word1, String word2, String word3) {
if (!wordIndex.containsKey(word1) || !wordIndex.containsKey(word2) || !wordIndex.containsKey(word3))
return -1;
List<Integer> indexList1 = wordIndex.get(word1);
List<Integer> indexList2 = wordIndex.get(word2);
List<Integer> indexList3 = wordIndex.get(word3);
int index1 = 0;
int index2 = 0;
int index3 = 0;
int minDis = Integer.MAX_VALUE;
while (index1 < indexList1.size() && index2 < indexList2.size() && index3 < indexList3.size()) {
int val1 = indexList1.get(index1);
int val2 = indexList2.get(index2);
int val3 = indexList3.get(index3);
if (val1 < val2 && val1 < val3) {
int manhattan_dis = val2 - val1 + val3 - val1 + Math.abs(val2 - val3);
minDis = Math.min(manhattan_dis, minDis);
index1++;
}
else if (val2 < val1 && val2 < val3) {
int manhattan_dis = val3 - val2 + val1 - val2 + Math.abs(val1 - val3);
minDis = Math.min(manhattan_dis,minDis);
index2++;
}
else {
int totalDis = val1 - val3 + Math.abs(val2 - val1);
minDis = Math.min(minDis, totalDis);
index3++;
}
}
return minDis;
}
public static void WordDistance(String[] words) {
for (int i = 0; i < words.length; i++) {
if (!wordIndex.containsKey(words[i]))
wordIndex.put(words[i], new ArrayList<>());
wordIndex.get(words[i]).add(i);
}
}
public int minDistance(String word1, String word2, String word3) {
if (!wordIndex.containsKey(word1) || !wordIndex.containsKey(word2) || !wordIndex.containsKey(word3))
return -1;
List<Integer> indexList1 = wordIndex.get(word1);
List<Integer> indexList2 = wordIndex.get(word2);
List<Integer> indexList3 = wordIndex.get(word3);
int index1 = 0;
int index2 = 0;
int index3 = 0;
int minDis = Integer.MAX_VALUE;
while (index1 < indexList1.size() && index2 < indexList2.size() && index3 < indexList3.size()) {
int val1 = indexList1.get(index1);
int val2 = indexList2.get(index2);
int val3 = indexList3.get(index3);
if (val1 < val2 && val1 < val3) {
int manhattan_dis = val2 - val1 + val3 - val1 + Math.abs(val2 - val3);
minDis = Math.min(manhattan_dis, minDis);
index1++;
}
else if (val2 < val1 && val2 < val3) {
int manhattan_dis = val3 - val2 + val1 - val2 + Math.abs(val1 - val3);
minDis = Math.min(manhattan_dis,minDis);
index2++;
}
else {
int totalDis = val1 - val3 + Math.abs(val2 - val1);
minDis = Math.min(minDis, totalDis);
index3++;
}
}
return minDis;
}
Similar topics
» Shortest Word DistanceII
» Shortest Word DistanceIII
» Word Distance Finder
» Word LadderII Each Time Delete每次删除一个字符
» Word Search I leetcode 原题 amazon onsite特喜欢考
» Shortest Word DistanceIII
» Word Distance Finder
» Word LadderII Each Time Delete每次删除一个字符
» Word Search I leetcode 原题 amazon onsite特喜欢考
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 数据结构的使用:使用HashMap:ShortestWordDistance系列(也有可能电话面试题)
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|