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. [Java] 例外處裡 try/catch & throws

    public class CheckException { public static void main(String[] args) { File file = new File("xx ...

  2. Vue 中使用 viewerjs

    安装 viewerjs npm install viewerjs --save 创建一个 Viewer.vue 组件 <template> <div id="index&q ...

  3. ubuntu彻底卸载opencv

    说正事之前,先啰嗦两句背景,算是拿个小本本记下了. 我本打算下载opencv2.4.在github上找到源码,在Branch处选择切换到2.4,然后复制URL,在terminal里面使用git clo ...

  4. 0003 - 基于xml的Spring Bean 的创建过程

    一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...

  5. lecune入门示例

    注意:本示例中的lucene版本需在jdk7以上使用. 一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  6. Java选择排序,插入排序,快速排序

      public class Test { public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5 }; 选择排序(a); ...

  7. mysql Mac篇

    默认为mysql下载和安装完毕,安装为默认安装 下载地址:https://dev.mysql.com/downloads/file/?id=473576 1.启动mysql sudo /usr/loc ...

  8. day40数据库之表的相关操作

    数据库之表的相关操作1.表的操作: 1.创建表的语法:        create table 表名(              id   int(10)   primary key auto_inc ...

  9. 搭建基于MySQL的读写分离工具Amoeba

    搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...

  10. Spring利用注解@Value获取properties属性为null

    今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...