[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为实现就是抄一遍:并非,往往需要用其它的数据结构

[英文数据结构或算法,为什么不用别的数据结构或算法]:

前向指针-右移窗口-字符串型。

[一句话思路]:

为了使得窗口尽量小,把窗口左边界往左移 (i j中较小的++)

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 既然是对index进行操作,就要从index的list里面再取一次index
  2. 求最小值时一般把result初始化为INT.MAX

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

不是while循环一直往右的,就是if条件下 较小的指针往右移就行了

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

实现类的题,类名和主函数名要相同。主函数有参数,新建类就要带参数。

在调用类里正常输入、输出变量就行了。

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class Solution {
//ini: hashmap
HashMap<String, List<Integer>> map = new HashMap<>();
//require the same signature, initialize with parameter if necessary
public Solution(String[] words) {
//store the words
for (int i = 0; i < words.length; i++) {
//contains key or not
if (map.containsKey(words[i])) {
map.get(words[i]).add(i);
}else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(words[i], list);
}
}
} public int shortest(String word1, String word2) {
//get list1, list2
List<Integer> list1 = map.get(word1);
List<Integer> list2 = map.get(word2);
int result = Integer.MAX_VALUE; //for loop, maintain a sliding window
for (int i = 0, j = 0; i < list1.size() && j < list2.size();) {
int index1 = list1.get(i);
int index2 = list2.get(j);
//minimum the difference
if (index1 < index2) {
result = Math.min(result, index2 - index1);
i++;
}
else {
result = Math.min(result, index1 - index2);
j++;
}
} //return
return result;
}
} class MyCode {
public static void main (String[] args) {
String[] words = {"practice","makes","perfect","coding","makes"};
Solution answer = new Solution(words);
String word1 = "practice";
String word2 = "practice";
int rst = answer.shortest(word1,word2);
System.out.println("rst= " + rst); /*int[] copy = answer.reset();
for (int i = 0; i < 8; i++)
System.out.println("copy[i] = " + copy[i]);*/
}
}

[潜台词] :

244. Shortest Word Distance II 实现数组中的最短距离单词的更多相关文章

  1. [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 ...

  2. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

  3. [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 ...

  4. [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 ...

  5. LC 244. Shortest Word Distance II 【lock, Medium】

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  6. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  7. [LC] 244. Shortest Word Distance II

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  8. [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 ...

  9. [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

随机推荐

  1. Python开发 基礎知識 2.變量 ( *arg, **kwargs )

    變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...

  2. Django 数据库迁移

    Django 数据库迁移 DATABASES = { # Django默认配置使用sqlite3数据库 # 'default': { # 'ENGINE': 'django.db.backends.s ...

  3. Java_集合_ArrayLish Comparator比较排序 小笔记

    import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Teacher ...

  4. MySQL Error--Error Code

    mysql error code(备忘) 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导 ...

  5. JS笔记汇总

      注释必须要多写! 1.方便后台看 2.方便自己查错和优化 事先先沟通约定好,比如交互的数据格式需求是怎么样的啊,功能模块的逻辑是怎么样的等等.提前先和产品还有后台沟通好.   JSON内不能包含注 ...

  6. Java高级特性 第5节 序列化和、反射机制

    一.序列化 1.序列化概述 在实际开发中,经常需要将对象的信息保存到磁盘中便于检索,但通过前面输入输出流的方法逐一对对象的属性信息进行操作,很繁琐并容易出错,而序列化提供了轻松解决这个问题的快捷方法. ...

  7. Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'

    错误: Reading package lists... Done E: Problem executing scripts APT::Update::Post-Invoke-Success 'if ...

  8. 2.3 Visio画虚线后插入word或PPT变为实线

    选中实线后,左键选择->格式->线条->粗细->自定义->设置为0pt

  9. Firebug 没死,活在 Firefox DevTools 中

    伯乐在线转注:2016年12月7日有一条<Firebug 宣布停止开发更新>的资讯,不少朋友误认为以后用不到 Firebug 了.其实在 2015 年 Firebug 已经在着手整合到 F ...

  10. VDMA时序分析

    VDMA时序分析