244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]:
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中较小的++)
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 既然是对index进行操作,就要从index的list里面再取一次index
- 求最小值时一般把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 实现数组中的最短距离单词的更多相关文章
- [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 ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [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 ...
- [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 ...
随机推荐
- LeetCode - Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- C# 利用反射完成计算器可扩展功能
一个主要的窗体程序,两个输入框,一个label using System; using System.Collections.Generic; using System.ComponentModel; ...
- PythonStudy——变量 Variable
变量 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问.在指令式语言中,变量通常是可变的:但在纯函数式语言(如Haskell)中,变量可能是不可变(immutab ...
- Maven项目中在properties 中使用 ${} 来引用pom文件中的属性
比如在pom文件中定义了属性如下: <jdbc.host.global>127.0.0.1</jdbc.host.global> <jdbc.databasename.g ...
- OpenStack上搭建Q版的公共环境准备(step1)
vmware14 centos7.5minimal版 controller1节点虚拟硬件配置: CPU:1颗2核 Memory:2G 硬盘:20G 网卡: VMnet1(仅主机模式):关闭DHCP,手 ...
- Mycat 镜像-创建 Docker 镜像
将 Mycat-server 创建到镜像,使其能够进行容器化部署,我们需要创建 Dockerfile 并在文件中安装其依赖项,使用 centos 做为 base 镜像,并安装 jdk 依赖即可,因此创 ...
- unity的一些tips
主要是我知乎上回答的一个关于unity的tip,备忘. 说说我所看到unity相关的,不好的习惯: 1 尽量不要在Awake(), start()等函数内加入业务逻辑的初始化代码.首先无法简便的直接启 ...
- 洛谷P1605走迷宫
传送 这是一道dfs,但是...但是....但是它竟然被放在bfs练习题辣!!!! 打了半天bfs,发现路径不会标记了,于是发现好像有什么不对的,似乎dfs要简单一点,于是半路跑去打dfs,结果打了半 ...
- centos7+nginx+rtmp+ffmpeg搭建流媒体服务器(保存流目录与http目录不要随意配置,否则有权限问题)
搭建nginx-http-flv-module升级代替rtmp模块,详情:https://github.com/winshining/nginx-http-flv-module/blob/master ...
- SQLSERVER创建该存储过程时不会出错,但是执行存储过程时报错
创建该存储过程时,不会出错,但是执行存储过程时,会报出下面这样的错误 这是因为在存储过程创建时,它先做语法检查,如果通过了语法检查,它会尝试解析它包含的对象名,如果存在也会解析该对象引用的对象是否存在 ...