LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/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.
题解:
method 会被call好多次,没有必要每次都traverse 一遍words array. 简历一个HashMap, key是word, value是该word出现index的list.
双指针遍历word1 和 word2 两个index list 计算最小距离.
Time Complexity: Constructor WordDistance O(words.length). shortest O(list1.size()+list2.size()).
Space: O(words.length).
AC Java:
public class WordDistance {
HashMap<String, List<Integer>> hm;
public WordDistance(String[] words) {
hm = new HashMap<String, List<Integer>>();
for(int i = 0; i<words.length; i++){
if(!hm.containsKey(words[i])){
List<Integer> ls = new ArrayList<Integer>();
hm.put(words[i], ls);
}
hm.get(words[i]).add(i);
}
}
public int shortest(String word1, String word2) {
List<Integer> l1 = hm.get(word1);
List<Integer> l2 = hm.get(word2);
int res = Integer.MAX_VALUE;
int i = 0;
int j = 0;
while(i<l1.size() && j<l2.size()){
int index1 = l1.get(i);
int index2 = l2.get(j);
if(index1<index2){
res = Math.min(res, index2-index1);
i++;
}else{
res = Math.min(res, index1-index2);
j++;
}
}
return res;
}
}
// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");
LeetCode Shortest Word Distance II的更多相关文章
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode#244] Shortest Word Distance II
Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
随机推荐
- win7 无法复制粘贴
win7 无法复制粘贴解决办法 在开始-运行里输入以下命令 regsvr32 shdocvw.dll ,确定,接着出现一个消息框,确定 regsvr32 oleaut32.dll,确定,接着出现一个 ...
- os模块
os模块 posix(unix) nt(win) mac import osprint(os.name) #nt os和sys的区别: os是负责程序和操作系统之间的交互. os.path (是一个 ...
- lucene 建立索引的过程
时间 -- :: CSDN博客 原文 http://blog.csdn.net/caohaicheng/article/details/ 看lucene主页(http://lucene.apach ...
- java---相亲练习
public class wu{ public static void main(String[] args){ //TODO 自动生成的方法存根 int a = 0,b = 0,c = 0; int ...
- Java表单类双击提交
双击制御 有些时候一些操作会非常的耗费时间(Long Lived Operation),例如这个数据库的导出,表表生成等.有些时候程序的使用者看到很长时间服务器 没有反应,倾向于多次点击提交按钮.这样 ...
- HDU 2795 Billboard(线段树的另类应用)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- VMware 虚拟机使用 NAT 方式联网
选择要设置的虚拟主机: 点击右键,选择 “属性”,查看 “网络适配器”: 此时选择的连接方式是 “Host-only”,在 Host-only 模式中,所有的虚拟系统是可以相互通信的,但虚拟系统和真实 ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- BOM表生成
- 大话数据结构(十一)java程序——串
1.串的定义 串(String):是由零个或多个字符组成的有限序列,又名为字符串. 一般记为s="a1a2a3.........an"(n>=0),其中,s是串名称,用双引号 ...