245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]:
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Input: word1 =“makes”, word2 =“coding”
Output: 1
Input: word1 ="makes", word2 ="makes"
Output: 3
[暴力解法]:
把word1所有的index存一个数组,把word2所有的index存一个数组,再i*j全部扫一遍
时间分析:n^2
空间分析:
[优化后]:
每次走一个index都随时更新一下i1 i2
时间分析:n
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
虽然不麻烦,但是Integer.MAX_VALUE 必须要存成long型,然后转回来就行了。不然会报错。
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
p1 p2相等的时候,暂存一下之前的p2
if (word1.equals(word2))
//store in p1 temporarily
p1 = p2;
p2 = i;
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
//ini: p1, p2 into the long type
long result = Integer.MAX_VALUE;
long p1 = Integer.MAX_VALUE;
long p2 = -Integer.MAX_VALUE;
//for loop: for each words[i], renew the answer
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) p1 = i;
if (words[i].equals(word2))
{
if (word1.equals(word2))
//store in p1 temporarily
p1 = p2;
p2 = i;
}
//renew the answer
result = Math.min(result, Math.abs(p2 - p1));
}
//return
return (int)result;
}
}
245. Shortest Word Distance III 单词可以重复的最短单词距离的更多相关文章
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- LC 245. Shortest Word Distance III 【lock, medium】
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
随机推荐
- qsort函数排序各种类型的数据。
qsort函数是库函数中的一员,我们先来看看官方文档是怎么写的: 其中qsort的参数void* base是传入一个数组,size_t num 为数组整体大小,size_t size 为单个元素的大小 ...
- jQuery基础(三)事件
1.鼠标事件 jQuery鼠标事件之click与dblclick事件 click方法用于监听用户单击操作,dbclick方法用于监听用户双击操作. 方法一:$ele.click() 绑定$ele元素, ...
- Visual Studio生成webservice代理类
首先点击 vs菜单栏->工具 ,选择 外部工具, 在弹出的窗口中点击 添加, 然后在“标题”行中输入"WSDL生成代理类", "命令"行中输入" ...
- 爬虫之PyQuery
PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同. 官方文档:http://pyquery.readthedocs.io/ 安装 pip insta ...
- 关于rtsp的时间戳问题
这里主要关注的rtp包的时间戳,在rtsp中,播放器的1S钟的定义是和媒体的采样率有关的. 例如视频的采样率是90K,那么最小时间粒度(单位)是1/90000秒,再转换成ms就是 1/90毫秒,这个就 ...
- echars柱状图修改每条柱的颜色
在实际开发中,可能会需要到柱状图内某个柱需要特殊颜色表示,我这里的应用是排名,突出显示出当前的数据. 在Color参数中添加一个方法可以任意返回需要的颜色值 function (params) { i ...
- 知识点:Java 内存模型完全解密
Java虚拟机(JVM) 规范中定义了一种Java的内存模型,即Java Memoory Model(简称JMM),用来实现让Java程序在各个平台下都能达到一致的内存访问效果. JVM是整个虚拟机, ...
- Pascal语言(存档)
数据类型 标准函数 运算符和表达式 输入语句 输出语句 if语句 case语句 for语句 while语句 repeat语句 函数与过程 形参与实参 全局变量与局部变量 数组 字符串 枚举 子界 集合 ...
- Java 基础测试题
一.选择题 1.下面哪些是合法的变量名? ( DEG ) A.2variable B. .variable2 C. ._whatavariable D._3_ E.$anothervar F.#my ...
- TCP的三次握手与四次挥手理解及面试题(很全面)
序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上序号后,就给每一个报文段指派一个序号:序列号seq就是这个报文 ...