243. Shortest Word Distance 最短的单词index之差
[抄题]:
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道用什么数据结构指定index
[一句话思路]:
指定index只需要设成-1然后遍历就行
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 头一次见:必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
字符串判断相等用的是equals,你没记错
[关键模板化代码]:
[其他解法]:
[Follow Up]:
244. Shortest Word Distance II 多次调用:hashmap多次取
3:可重复:用数学
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
//cc
if (words == null || words.length == 0) {
return 0;
}
//ini
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE;
//for loop, renew p1 p2, compare
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
p1 = i;
}
if (words[i].equals(word2)) {
p2 = i;
}
if (p1 != -1 && p2 != -1) {
min = Math.min(min, Math.abs(p2 - p1));
}
}
//return
return min;
}
}
243. Shortest Word Distance 最短的单词index之差的更多相关文章
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [leetcode]243. Shortest Word Distance最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- 243. Shortest Word Distance
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...
- 【LeetCode】243. Shortest Word Distance 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LC] 243. Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- [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 ...
随机推荐
- hibernate的list和iterate的区别
一.先介绍一下java中的缓存系统JCS(java cache system) 1.JCS(Java Caching System)是一个对象Cache,它可以把Java对象缓存起来,提高那些访问频 ...
- Android Studio 打包及引用 AAR(可能是史上最详细的 )
https://www.jianshu.com/p/1777a634db5e Android Library(AAR) 的好处 Android 库在结构上与 Android 应用模块相同.它可以提供构 ...
- c#联网判断
引用命名空间:sing System.Net.NetworkInformation; var address = "www.baidu.com"; Ping ping = null ...
- SSM框架——整合搭建流程
1.首先创建maven工程,使用哪种方式进行创建都可以,可以参考博主之前的文章: <两种方式创建Maven项目[方式二]><两种方式创建Maven项目[方式一]> 2.先看看搭 ...
- sublime文件对比插件--sublimerge
网上很多文件对比的基本都要收费,所以还是干脆看看sublime有没插件算了. 结果还是有一个:sublimerge 1 先安装该插件: 2 然后在sublime下都打开要对比的两个文件: 3 然后在其 ...
- windows下通过.bat运行java程序
在windows下运行Java项目,单独的jar可以使用,java -jar xxx.jar 运行,如果是一个zip包,里面包含了class文件和所依赖的jar的时候,可以使用 (也可以以看看这里): ...
- 分表分库之二:唯一ID的生成方法
一.为什么要全局唯一? 我们在对数据库集群作扩容时,为了保证负载的平衡,需要在不同的Shard之间进行数据的移动, 如果主键不唯一,我们就没办法这样随意的移动数据.起初,我们考虑采用组合主键来解决这个 ...
- Java-Maven-Runoob:Maven 项目文档
ylbtech-Java-Maven-Runoob:Maven 项目文档 1.返回顶部 1. Maven 项目文档 本章节我们主要学习如何创建 Maven 项目文档. 比如我们在 C:/MVN 目录下 ...
- 学习笔记::fft
上次学fft还是5月份,昨天发现已经忘记怎么推导了,代码也看不懂了,就又学习了一发,大概是看menci的博客 0.fft可以进行多项式乘法,朴素的乘法跟手算一样是O(n^2),fft可以通过分治做到n ...
- JS四级复选框选中层次关系
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...