[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 ...
随机推荐
- 自己封装一个弹窗JS
在我们平时的开发中,一定有很多地方需要用到弹窗提示功能.而系统自带的弹窗奇丑无比,而且我们不能自主控制.因此我们在开发过程中,自己封装一个弹窗JS方便我们使用. 代码demo如下: // JavaSc ...
- Nginx网站实现ssl安全套接字
nginx.conf配置 server { listen 443 ssl; server_name www.example.com; ssl on; ssl_certificate cert.pem; ...
- Flutter 知识点
Flutter:一个移动应用开发框架,它使用 Dart.C++.Skia 开发,对外提供了完全不依赖系统平台的 Widget 的能力,只通过自绘图形的方式工作,具有极其优秀的跨平台性.目前已经支持了 ...
- golang http proxy反向代理
本文介绍golang中如何进行反向代理. 下面例子中, proxy server接收client 的 http request,转发给true server,并把 true server的返回结果再发 ...
- docker镜像制作 centos6 nginx1.15.6 with NGINX_UPSYNC_MODULE
首先我选择了在centos6里部署nginx的镜像,如果大家选择的是centos7,自己重新修改吧 这里的问题点有几个: 1,make的版本选择,因为我下载了最新的cmake,需要c++11编译 这玩 ...
- FileProvider相关 Failed to find configured root that contains
问题: 使用FileProvider构造SD卡中文件uri时异常 java.lang.IllegalArgumentException: Failed to find configured root ...
- 1、ZooKeeper 基本概念、使用方法、实践场景
ZooKeeper 基本概念 ZooKeeper 是面向分布式应用的协调服务,其实现了树形结构的数据模型(与文件系统类似),并且提供了简洁的编程原语.ZooKeeper 能够作为基础,用于构建更高层级 ...
- Error Code: 1786 Statement violates GTID consistency: CREATE TABLE ... SELECT.
1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:call account_check_main('20180511') 错误 ...
- 从神经网络到卷积神经网络(CNN)
我们知道神经网络的结构是这样的: 那卷积神经网络跟它是什么关系呢?其实卷积神经网络依旧是层级网络,只是层的功能和形式做了变化,可以说是传统神经网络的一个改进.比如下图中就多了许多传统神经网络没有的层次 ...
- DevExpress的42种窗体样式
在Winform环境下DevExpress标题栏皮肤 第一步:引用DLL文件,安装DevExpress后在引用>程序集>扩展: DevExpress.BonusSkins.v12.2.dl ...