[LC] 244. Shortest Word Distance II
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. Your method will be called repeatedly many times with different parameters.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Input: word1 =“coding”, word2 =“practice”
Output: 3
Input: word1 ="makes", word2 ="coding"
Output: 1
class WordDistance {
private Map<String, List<Integer>> mymap;
public WordDistance(String[] words) {
mymap = new HashMap<>();
for (int i = 0; i < words.length; i++) {
if (mymap.containsKey(words[i])) {
mymap.get(words[i]).add(i);
} else {
List lst = new ArrayList<>();
lst.add(i);
mymap.put(words[i], lst);
}
}
}
public int shortest(String word1, String word2) {
List<Integer> lst1 = mymap.get(word1);
List<Integer> lst2 = mymap.get(word2);
int i = 0, j = 0, res = Integer.MAX_VALUE;
while (i < lst1.size() && j < lst2.size()) {
res = Math.min(res, Math.abs(lst1.get(i) - lst2.get(j)));
if (lst1.get(i) < lst2.get(j)) {
i += 1;
} else {
j += 1;
}
}
return res;
}
}
/**
* Your WordDistance object will be instantiated and called as such:
* WordDistance obj = new WordDistance(words);
* int param_1 = obj.shortest(word1,word2);
*/
[LC] 244. Shortest Word Distance II的更多相关文章
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- [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]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 ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
- 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- [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 II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
随机推荐
- 解压版mysql安装步骤
第一步:解压mysql,例如解压后的目录在C:\Program Files\mysql-5.6.36-winx64 第二步:配置mysql环境变量,类似配置java的环境变量 计算机右键===> ...
- linux 替换jdk指定jar包
我的bug是:jdk1.8的安全策略和腾讯邮箱服务有冲突.我知道的解决方法: 1更换低版本安全策略相关的jar包.(windows:http://www.cnblogs.com/dennyzhangd ...
- UVA 11468 AC 自动机
首先我们应该是枚举 L个位置上的每个字符来得到最终概率 然后AC自动机的作用就是为了判断你枚举的地方是否对应了单词节点,如果对应了,就肯定要不得 #include <iostream> # ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- MyBatis中foreach传入参数为数组
一.当只有一个参数,并且这个参数是数组时 接口方法的参数不需要添加@Param注释,collection="array" 示例 接口的方法 void deleteMulti(Str ...
- Django的模板变量
变量的值来自context中的数据字典, 类似于字典对象的keys到values的映射关系. 变量是被}}和{{括起来的部分 变量看起来就像是这样: {{ variable }}. 当模版引擎遇到一个 ...
- 苹果浏览器移动端click事件延迟300ms的原因以及解决办法
这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题 —— 当时的网站都是为大屏幕设备所设计的.于是苹果的工程师们做了一些约定,应对 iPhone 这种小屏幕浏览桌面端站点 ...
- 通过OAuth2.0 获取授权访问SF 用户数据
站长资讯: 创建应用程序 新建应用程序 访问示例(Python+django) 环境准备: index.html 两种方式: 方式一:采用由用户授权,调用者无需知道SF的用户名与密码 方式二:直接 ...
- ios ktvhttpcache 音视频缓存插件使用
1.PodFile 文件增加 pod 'KTVHTTPCache', '~> 2.0.0' 2.在终端 需要先cd到podfile文件所在目录 执行pod install 3.在header ...
- vue wangeditor3封装
<script src="wangEditor/3.1.1/wangEditor.min.js"></script> Vue.component('my ...