[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 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)的更多相关文章
- [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 ...
- [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 ...
- [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 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- [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 ...
- 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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
随机推荐
- PythonStudy——字符串、列表、元组、字典、集合 (str、list、tuple、dict、set)常用方法总结
字符串: # 字符串类型的一些操作(str)st1='hello world 'print(st1[0]) # 根据字符串索引来取字符h 找不到直接崩print(st1[-1]) # 根据索引倒取st ...
- openstack--9--深入理解虚拟机
登录计算节点查看进程 [root@linux-node2 ~]# ps aux | grep kvm root 824 0.0 0.0 0 0 ? S< 10:19 0:00 [kvm-irqf ...
- Django 小饭桌项目实战笔记
gulp-sass安装 安装报错,原因未设置全局镜像源npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ ...
- VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql 1.参考以前博文,恢复在 Vs2010+linQ for Mysql的环境. 2.建立 wpf工程,参照1,生成 datacontext.cs , ...
- linux-linnode满了的提示
线上有一台web服务器磁盘检测告警了,提示空间不足,登到服务器查看 <ignore_js_op> touch:cannot touch `furm.html': No space left ...
- Ubuntu 12.04图形界面不能登录问题
问题描述: Ubuntu 12.04进入到登录界面,输入用户名和密码无法登录, 输出密码后又跳回到登录界面, 执行快捷键Ctrl+Alt+F1, 可以进入tty1命令行, 可以root或者普通用 ...
- JsonPath 使用
Map<String, String> map ----> $.store.bicycleString str = $.store.otherList<Map<Str ...
- Flask--(一对多demo)作者书籍模型
一对多模型的增加和删除 后端实现: from flask import Flask from flask import flash from flask import redirect from fl ...
- 使用IDE之webstorm
最近打算试试用webstorm,今天从vscode换成了webstorm. 官方下载webstorm 1.下载之后安装,我全部选择默认,因为webstorm是付费ide,到启动面板时,选择激活选项. ...
- 软件-集成开发环境:IDEA(Java 语言开发的集成环境)
ylbtech-软件-集成开发环境:IDEA(Java 语言开发的集成环境) IDEA 全称IntelliJ IDEA,是用于java语言开发的集成环境(也可用于其他开发语言),IntelliJ在业界 ...