[抄题]:

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. centos7生产环境下openssh升级

    由于生产环境ssh版本太低,导致使用安全软件扫描时提示系统处于异常不安全的状态,主要原因是ssh漏洞.推荐通过升级ssh版本修复漏洞 因为是生产环境,所以有很多问题需要注意.为了保险起见,在生产环境下 ...

  2. dubbo 基础入门

    一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...

  3. Python小练习(一)

    1:有一个列表,其中包括10个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最后,然后输出这个列表.最终样式是[2,3, ...

  4. Linux内核分析第九次作业

    理解进程调度时机跟踪分析进程调度与进程切换的过程 一.基础知识 Linux系统的一般执行过程 一般情况:正在运行的用户态进程X切换到运行用户态进程Y的过程 1. 正在运行的用户态进程X 2. 发生中断 ...

  5. 计算:表中varchar类型的字段能容纳的最大字符数?

    建表语句: CREATE TABLE `test2` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `content` varchar(21842) NOT ...

  6. Linux whereis命令详解

    Linux whereis命令 Linux whereis命令用于查找文件. 该指令会在特定目录中查找符合条件的文件.这些文件应属于原始代码.二进制文件,或是帮助文件. 该指令只能用于查找二进制文件. ...

  7. python 前后端分离 简单的数据库返回接口

    1.使用node http-server 起本地服务器  或者打开nginx 直接用nginx的默认页面也可以 (用下面的html文件替换nginx下html文件夹下的index.html) http ...

  8. Browser Render Engine & Javascript Engine

    Browser Render Engine Programming Language Open Source Javascript Engine Comparation for CSS Compati ...

  9. IIC时序详解

    Verilog IIC通信实验笔记 Write by Gianttank 我实验的是 AT24C08的单字节读,单字节写,页读和页写,在高于3.3V系统中他的通信速率最高400KHZ的,我实验里用的是 ...

  10. [UE4]Scale Box:缩放容器

    一.Scale Box只能有一个子控件,再拖放一控件进去是不行的. 二.Scale Box缩放保持长宽比例 三. Scale Box.Strectching.Strectch:拉伸设置.  Scale ...