[抄题]:

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. ASP.NET资源大全-知识分享 【转载】

    API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...

  2. 20165308 2017-2018-2 《Java程序设计》课程总结

    20165308 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期待的师生关系 学习基础和c语言调查 Linux 安装及学习 第一周学习总结 第二 ...

  3. MNIST数据可视化

    一.数据准备 二.数据说明 可以看出图片数据在偏移量为第16字节开始存,每28X28字节存放一张手写字图片.而label是从偏移量为第8字节开始存,每个字节存放一个label. 三.matlab201 ...

  4. plot

    scatter import pandas as pd df_train=pd.read_excel(r"C:\Users\Liugengxin\Desktop\回归.xlsx") ...

  5. EasyMock 模拟对象测试

    一.EasyMock 使用动态代理实现模拟对象创建,一般可以满足以下测试需求 1.要测试的模块依赖于其它自己控制不了的模块,如第三方服务,其它组员在开发的服务等,它们都没办法配合你来测试: 2.涉及到 ...

  6. centos 设置中文环境

    方法1: [hl@localhost ~]$ LANG=zh_CN.UTF-8 #只对当前shell有效,临时设置 [hl@localhost ~]$ ll 总用量 drwxrwxr-x. hl hl ...

  7. Let'sencrypt.sh 抛出异常: Response: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)>

    起因 今天网站的SSL证书过期了,打算重新申请,运行 Let'sencrypt.sh 的时候抛出了这么个异常. 一番搜索,发现居然找不到直接的答案.没有直接的答案就只能通过间接的答案来解决了. 希望我 ...

  8. gentoo 建立本地软件库并安装软件 Custom repository

    参考 Handbook:AMD64/Portage/CustomTree https://wiki.gentoo.org/wiki/Handbook:AMD64/Portage/CustomTree# ...

  9. python 列表复制给另一个列表,改值两个列表均会改变(备忘)

    http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B为一个列表,结果在后续对A的操作中,导致B中的值也改变了,才回忆起 ...

  10. leetcode15

    class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...