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的更多相关文章

  1. 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 ...

  2. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 244. Shortest Word Distance II 实现数组中的最短距离单词

    [抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...

  7. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  8. [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 ...

  9. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

随机推荐

  1. 动态改变tableHeaderView的显示隐藏及高度

    改变tableHeaderView的高度: UIView *headerView = _tableView.tableHeaderView; headerView.height = 10; 当设置高度 ...

  2. nginx 负载策略

    1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream bak ...

  3. Dp(NOIp级)全解

    2018年(你还真以为我会讲保卫王国2333 LuoguP5020 货币系统 这道题就相当于求{A}的线性基大小 证明: 反证法,设该解为B,那么B定能表示出{A}的线性基,即{A}的线性基中所有数都 ...

  4. Anaconda 添加清华源与恢复默认源

    1.添加清华源 查看清华大学官方镜像源文档:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 本文基于Windows平台,首先用命令: conda ...

  5. javaweb学习——JDBC(五)

    管理结果集 JDBC使用ResultSet来封装查询到的结果集,然后移动记录指针来取出结果集的内容,除此之外,JDBC还允许通过ResultSet来更新记录,并提供了ResultSetMetaData ...

  6. 初学C#之运算符和关系表达式

    ㈠运算符和关系表达式 一元运算符++.-- 前加和后加区别,事例++在后如下: ; ; //结果age=19 说明age++在表达式中age的值也+1.结果sum=8,原因age++,++在后用age ...

  7. tensorflow实现卷积层的几种方式

    #coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...

  8. nginx反代及后端web配置

    一.反代配置,proxy_pass指向的upstream需要在反代的nginx.conf中配置 server {access_log /home/nginx/front_access.log;erro ...

  9. Python笔记_第四篇_高阶编程_GUI编程之Tkinter_6.附录

    1. 事件附录: 2. 事件属性附录:

  10. 33)new和delete关键字

    ---------------------------------------------------------------------------------------------------- ...