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

题意:

还是数组中两个单词的最短距离。

相对于之前[leetcode]243. Shortest Word Distance最短单词距离

这题要求对于function,允许连环call(言外之意是,不能naive的扫数组了)

Solution1: HashMap + Merge Sort

1.  Since "method will be called repeatedly many times", it will cost much time to scan the whole input String array again and again.

We can use a hashmap in advance, saving each string as a key and its corresponding indices as a value.

2. Use pointer i, j to scan list1, list2 seperately, updating the shortest distance.

code

 class WordDistance {
HashMap <String, List<Integer>> map; public WordDistance(String[] words) {
map = new HashMap<>();
for(int i = 0 ; i< words.length; i++){
String w = words[i];
if(map.containsKey(w)){
map.get(w).add(i);
}else{
List<Integer> list = new ArrayList<>();
list.add(i);
map.put(w, list);
}
} } public int shortest(String word1, String word2) {
List<Integer> l1 = map.get(word1);
List<Integer> l2 = map.get(word2);
// --------------merger sort 思想 | ------------------------
// --------------merger sort \|/ ------------------------
int result = Integer.MAX_VALUE;
int i = 0;
int j = 0;
while(i<l1.size() && j<l2.size()) {
result = Math.min(result, Math.abs(l1.get(i)- l2.get(j)));
if(l1.get(i)<l2.get(j)){
i++;
}else{
j++;
}
}
// --------------merger sort------------------------
return result;
}
}

复杂度:

时间
存储:O(N)     map是用来存储的。用时为扫一遍given array的时间。 
查找:O(m+n) list是用来查找的。用时为扫list1的时间+扫list2的时间。

空间
存储:O(N)     map是用来存储的。将whole array的元素都放入了map。 (内心OS:map的空间是由key的个数决定的)
查找:O(N)     list是用来查找的。 list所占空间为each string的corresponding

[leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)的更多相关文章

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

  2. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

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

  4. LeetCode 243. Shortest Word Distance (最短单词距离)$

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

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

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

  7. 244. Shortest Word Distance II

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

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

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

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

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

随机推荐

  1. 我发起了一个 操作系统 GUI 和 Tcp / IP 包 的 开源项目 DeviceOS

    操作系统 如果 不需要 处理 复杂多样 的 硬件 兼容性, 其实 并不算 大项目, 可以算 毕业设计 . 但是, GUI 和 Tcp / IP  这两个 部分 的 实现逻辑 很多 很复杂,  这  2 ...

  2. 【剑指offer】数组中只出现一次的数字

    题目:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 思路1:使用HashMap存上所有的数字,数字作为Key,Value为对应的出现次数.这种做法可以 ...

  3. centos7下安全访问远程服务器

    1. 添加普通账号 众所周知,linux下的root拥有最高权限,可以执行任何命令.在使用root身份操作时,有时的一个不注意就可能将非常重要的删除(最可怕的是 rm -rf /).而linux不像w ...

  4. nginx http转 https

    场景 项目前期使用http,后期为了安全方面的考虑,启用了https.项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡.实际上只需要在nginx上启用https即可,使客户端与 ...

  5. python:推导式套路

    推导式套路 列表推导式为例的推导式详细格式,同样适用于其他推导式 variable = [out_exp_res for out_exp in input_list if out_exp == 2] ...

  6. MySQL安全机制 DDL DCL

    一.MySQL用户管理 1. 修改用户密码 ===root修改自己密码=== 方法一: # mysqladmin -uroot -p'123' password 'new_password' //12 ...

  7. (转)EF5+SQLserver2012迁移到EF6+mysql5.5.47

    原文地址:https://www.cnblogs.com/tinyjian/p/6235014.html:https://www.cnblogs.com/tinyjian/p/6235397.html ...

  8. Struts2 xxAction-validation.xml使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...

  9. Python高级技巧:用一行代码减少一半内存占用

    我想与大家分享一些我和我的团队在一个项目中经历的一些问题.在这个项目中,我们必须要存储和处理一个相当大的动态列表.测试人员在测试过程中,抱怨内存不足.下面介绍一个简单的方法,通过添加一行代码来解决这个 ...

  10. autocomplete input

    <html> <head> <title>jQuery UI Autocomplete - Combobox</title> <link rel= ...