[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 ...
随机推荐
- CTF-域渗透--HTTP服务--命令注入1
开门见山 1. 扫描靶机ip,发现PCS 192.168.31.210 2. 用nmap扫描开放服务和服务版本 3. 再扫描全部信息 4. 探测http服务的目录信息 5. 再用dirb扫描 6. 查 ...
- mysql出现 too many connections
出现这个问题的原因网上大致都是说这三种 1.慢sql 2.大量持久性的连接 3.程序没有及时关闭连接 解决方式 mysql -u 账号 -p 输入密码 show processlist; kill掉s ...
- 代码杂谈-SQL中的rank&row_number函数
两个函数细节记不住. 写个例子备注一下. select no, name, score , rank() over(partition by no order by score asc) rk1 , ...
- windows更新系统系统时无法更新
win7系统显示“windows update当前无法检查更新,因为未运行服务,您可能需要重新启动计算机” 解决方法 1.win+r打开运行窗口,输入CMD 2.在dos窗口中输入命令“net sto ...
- Java if、switch语句,break,case,类型转换、常量、赋值比较、标识符(2)
if语句: /* if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2: 三元运算符: 好处:可以简化if else代码. 弊端:因为是一个运算符,所以运算完必须要有一个结果 ...
- 快速dns推荐
中国互联网络中心:1.2.4.8.210.2.4.8.101.226.4.6(电信及移动).123.125.81.6(联通) 阿里DNS:223.5.5.5.223.6.6.6 googleDNS:8 ...
- 吴裕雄--天生自然Linux操作系统:Linux 系统启动过程
linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立终端 . 用户登录系统. in ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】
本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...
- Java中String常用方法总结
package cn.zhang.Array; /** * String类的一些常用方法 * @author 张涛 * */ public class TestString { public stat ...
- JavaSE--【转】网络安全之证书、密钥、密钥库等名词解释
转载 http://www.cnblogs.com/alanfang/p/5600449.html 那些证书相关的名词解释(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等 ...