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 ...
随机推荐
- UEFI+GPT双硬盘安装Win10+Ubuntu16.04双系统
转载请注明出处:http://www.cnblogs.com/willnote/p/6725594.html 安装环境 SSD+HDD双盘,Win10安装在SSD里,HDD分出来60G安装Ubuntu ...
- PythonStudy——字符串类型 String type
# 1.定义# 需求:你是"好学生" s1 = "你是\"好学生\"" print(s1) # 可以通过引号的嵌套,使内部不同的引号在不转义 ...
- Python3之set, frozenset记录
set1 = set([1, 2, 3, 4]) set2 = frozenset([1, 2, 3, 4]) print(set1, set2, sep='|||') set1.add(" ...
- Docker安装(一)
环境:CentOS release 6.9 (Final) 1.检查环境是否支持安装docker 1)系统内核是否是3.8或更高版本 uname -a (这个安装不了,内核版本不够) Linux ...
- bootstrap modal 点击头部移动
$(".modal").each(function(){ $(this).draggable({ handle: ".modal-header" // 只能点击 ...
- Flask--(登录注册)抽取视图函数
视图函数抽取: 在info目录下准备视图业务模块包:modules 在modules中添加首页模块包index 在index包的__init__中导入蓝图 在index的__init__创建蓝图 在i ...
- 几种不同格式的json解析
原文地址:http://blog.csdn.net/whx405831799/article/details/42171191 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是 ...
- .net mvc 分页
1.分页实体类 public class PageDto { public int PageIndex { get; set; } public int PageSize { get; set; } ...
- Vue--父子组件之间的传值
1.父传子 1.在父组件中给子组件绑定动态属性 <v-home :title="title"></v-home> 2.在子组件中用propos[]接收 ex ...
- jvm常见的面试题
1. 内存模型以及分区,需要详细到每个区放什么. 2. 堆里面的分区:Eden,survival from to,老年代,各自的特点. 3. 对象创建方法,对象的内存分配,对象的访问定位. 4. GC ...