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. Hyper-V 连网备忘

    最近总是把简单问题复杂化 1.内网连接: Hyper-V 上网一个内网连接足矣,宿主机与客户机可以互访,而且都可以上网.创建一个内网连接,默认配置界面如下: 然后把本地网卡共享给这个新建的内网连接,把 ...

  2. 第二章 C#语法基础 (2.2 C#语言的运算符和表达式)

    [案例]本案例通过随机数发生器随机产生三条边,要求输出三天边长(边长长度为1~20的整数),并判断是否可以构成一个三角形. 如果可以,则计算出三角形面积,否则输出信息”三条随机的边不能构成三角形“. ...

  3. Xilinx 7 series FPGA multiboot技术的使用

    Xilinx 7 series FPGA multiboot技术的使用 当升级程序有错误的时候,系统会启动golden bitstream 注意:需要在源工程与升级工程中添加如下约束语句 生成组合mc ...

  4. debian删除i386的包

    sudo apt-get remove --purge `dpkg --get-selections | grep i386 | awk '{print $1}'`; sudo dpkg --remo ...

  5. Windows系统中监控文件复制操作的几种方式

    http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...

  6. SpringSecurity-UsernamePasswordAuthenticationFilter的作用

    UsernamePasswordAuthenticationFilter应该是我们最关注的Filter,因为它实现了我们最常用的基于用户名和密码的认证逻辑. 先看一下一个常用的form-login配置 ...

  7. python3学习笔记四(列表1)

    参考http://www.runoob.com/python3/python3-list.html 序列 python包含6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和x ...

  8. ffmpeg 编译

    下载FFmpeg git clone https://git.ffmpeg.org/ffmpeg.git 配置编译FFmpeg ./configure --prefix=host --enable-s ...

  9. 网页提示504 gateway time-out是什么意思?如何解决?

    大家在访问网站的时候通常会遇到502错误.404错误等,很少会遇到504错误.但是在我们去访问大流量或者内容数据量较多的网站时,打开网页偶尔就会出现504 gateway time-out,这到底是什 ...

  10. PL2303 USB转串口 com

    PL2303 USB转串口 com PL-2303 XA/HXA chip http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225&pc ...