[抄题]:

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. zombodb sql functions 说明

    zombodb 提供了好多方便的sql 函数工具类以及帮助函数 查看zombodb 版本zdb.version() select * from zdb.version(); version ----- ...

  2. “数据上帝” Jeff Hammerbacher

    出生于1983年的数学天才Jeff Hammerbacher在23岁时加入了Facebook,一手组建起数据分析队伍.他是“数据科学”(data science)一词的提出者之一,被人们称为“数据上帝 ...

  3. .Net MVC TextBoxFor 扩展 placeholder 与 class 属性

    namespace System.Web.Mvc { public static class HtmlHelperExtensions { public static MvcHtmlString Bs ...

  4. Red-Gate.NET.Reflector.v8.0.1.308(内含注册机Keygen与注册图解)

    Red-Gate.NET.Reflector.v8.0.1.308(内含注册机Keygen与注册图解)   反编译神器 内含软件安装包.注册机及插件集合. 这里说下注册方法,注意不要在联网的情况下注册 ...

  5. EXCEL统计不重复值的数量

    如这一列中,有多少不重复值? 1.可以点击,数据,删除重复项,清除重复值,然后剩下的统计一下即可知道:       ===> 2.用公式:=SUMPRODUCT((MATCH(E3:E20,E3 ...

  6. Win7系统安装Centos7.0双系统(三)

    4.6语言选择 4.7安装信息设置,除以下几项改动其他都可默认. 软件选择(默认最小):带GUI的服务器或GNOME桌面,可根据使用需要选择安装软件. 磁盘分区:Linux默认可分为3个分区,分别是b ...

  7. Ajax请求的几个小练习

    Ajax请求的几个小练习 准备工作 路由中做了分发: re_path('^app01/',include('app01.url')) app01中url.py文件的内容: from django.ur ...

  8. 自己动手制作的淘宝闲鱼APP宝贝数据采集工具软件

    之前做过淘宝PC端宝贝和店铺数据的采集,后来需要做APP端的数据采集,因为没有学过Android,以前也都是做PC端的软件,有没有其他方法呢? 突然想到了用手机模拟器,可以在电脑端控制运行手机APP端 ...

  9. Linux背背背(1)

    目录: 1.文件目录 2.基本语法 3.centos7之后的版本中查看ip地址 4.~ 5.修改时间 ------------------------------------------------- ...

  10. struts设置开发者模式

    struts设置开发者模式 在使用ssh框架做项目时,struts.xml文件总要配置许多项功能,其中一个就是开发者模式: <constant name="struts.devMode ...