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. contos7 共享文件夹开机自动挂载

    网上很多文章都说改文件/etc/fstab 我试了很多次都不行 然后看到另一个方法 在/etc/rc.d/rc.local 增加挂在脚本这个时候要注意执行权限问题 我是这样做的 sudo mount ...

  2. dp--区间dp P1880 [NOI1995]石子合并

    题目描述 在一个圆形操场的四周摆放 N 堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出一个算法,计算出将 N 堆石子 ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习:Date(日期) 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 把Android studio的日志导入目标文件中

    最好是在Android studio的命令行工具中进行命令操作. adb logcat -v time > /Users/z/log.txt adb logcat -v time > /U ...

  5. Python创建命令行应用的工具 tools for command line application in python

    工具1:Docopt 地址:http://docopt.org/ 这个工具是根据模块的文档注释来确定参数的.注释分为两部分:Usage, option. \``` Usage: naval_fate ...

  6. 销售de经典语录

    [销售的境界] 1.顾客要的不是便宜,而是感觉上占了便宜: 2.不要与顾客争论价格,要与顾客讨论价值: 3.没有不对的客户,只有不够好的服务: 4.卖什么不重要,重要的是怎么卖: 5.没有最好的产品, ...

  7. C#压缩解压zip 文件

    /// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...

  8. dotnet core 链接mongodb

    导入命名空间 using MongoDB.Bson; using MongoDB.Driver; 测试示例: var client = new MongoClient("mongodb:// ...

  9. Shell语法 【if while for】

    [if语法 测试条件 判断语句] 转自:http://lovelace.blog.51cto.com/1028430/1211353 [while for循环] 转自:https://blog.csd ...

  10. JavaSE--异常信息打印

    最近项目用到第三方jar包,抛出运行时异常,打在日志用的 方法.得到的错误描述并不详尽,遂想到平时用的 发现其可以重定向输出,平时用流多是和文件相关,但是在当前背景下用文件打开流显得不是很合适,翻了下 ...