[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 ...
随机推荐
- jsp后台取出request请求头
请求发到a2这个servlet 在这个servlet中请求转发到index.jsp 在jsp中如下的java代码 Enumeration headernames=request.getHeaderNa ...
- zombodb 索引创建
索引的创建是zombodb 的核心,我们都是需要先创建table,然后创建索引,创建的时候我们可以指定es 集群的地址,同时可能需要使用 一些地址api(比如数据直接存储在es 中而不是pg 中) ...
- liunx学习笔记(一:常用命令)
linux: 在学习linux之前我们应该多少了解windows的一些相关操作,linux也就是类似windows的另一种操作系统,用来管理软硬件的一种应用.在windows下你可以通过鼠标点击相关的 ...
- 记录安装 java 环境,部署环境变量遇到的小坑
情况:先安装 jdk 7,再安装 jdk8,发现 java 的环境自动变成了 jdk8 解决: 1.在 系统的环境变量下,多出了一行: C:\Program Files (x86)\Common Fi ...
- paramiko实现上传目录
使用paramiko上传目录,重点是上传的那个过程,想了一上午才想出来,可能有点奇葩,但是还是实现了这个功能 #!/usr/bin/python import paramiko import os d ...
- Eclipse Build path
Build Path用于设置Java的构建路径,管理Java工程所包含的资源,使工程结构清晰合理. 包括以下几项: Source Source包括 source folder和output folde ...
- django模板语言循环字典,及forloop
views: from django.shortcuts import render,redirect from django.shortcuts import HttpResponse # Crea ...
- Windows 下最佳的 C++ 开发的 IDE 是什么?
作者:渡世白玉链接:https://www.zhihu.com/question/19589089/answer/30312199来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- 《Java程序设计》 第二周学习总结
20175334 <Java程序设计>第二周学习总结 教材学习内容总结 了解Java编程风格 认识Java基本数据类型与数组 掌握Java运算符.表达式和语句 教材学习中的问题和解决过程 ...
- python:数据类型dict
一.字典 key -->value 储存大量数据,而且是关系型数据,查询速度非常快 数据类型分类: 可变数据类型:list , dict, set 不可变的数据类型:int , bool, st ...