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 ...
随机推荐
- Centos python 2.6 升级到2.7.3
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 sudo make all sudo mak install sudo mak ...
- Ubuntu SSH root user cannot login
Open /etc/ssh/sshd_config and check if PermitRootLogin is set to yes. If not, then set it to yes and ...
- ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证
ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...
- Grunt - 前端开发所见即所得
首先要安装全局的grunt-cli sudo npm install grunt-cli -g 1,项目中初始化npm文件,项目名不要和[关键词]如grunt重复,一直回车即可 npm init 2, ...
- post可以直接把get请求代入到目标url中
Feigong --非攻 非攻 取自<秦时明月>--非攻,针对不同情况自由变化的武器 Feigong,针对各种情况自由变化的mysql注入脚本 Feigong,In view of the ...
- 将url的查询参数解析成字典对象
1, 这个题目不约而同的出现在了多家公司的面试题中,当然也是因为太过于典型,解决方案无非就是拆字符或者用正则匹配来解决,我个人强烈建议用正则匹配,因为url允许用户随意输入,如果用拆字符的方式,有任何 ...
- 使用HIBERNATE的SQL查询并将结果集自动转换成POJO
在某些场合下,我们可能想使用HIBERNATE的框架提供的SQL查询接口,但是,由于实体没有做映射,HIBERNATE不能把结果集转换成你想要的List<POJO>,本文讨论如何在这种情况 ...
- Ubuntu 12.04 LTS 及ubuntu14.10 -- NFS安装
在Linux 服务器上配置好NFS 根文件系统后,在单板侧挂载NFS 文件系统,具体操作如下:ifconfig eth0 hw ether 00:10:85:18:01:84 /*配置MAC地址*/i ...
- eclipse中的项目Java build path (Java创建路径)详解
1.Source标签页,指定本工程的源码目录和输出目录.Projects标签页,指定本工程所依赖的其他工程.Libraries标签页,指定本工程所需的jar包和class目录等.Order And E ...
- centos 使用pip安装mysql-python
直接使用# pip install mysql-python 命令报 EnvironmentError: mysql_config not found 错误 安装mysql-phthon 需要用的my ...