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 ...
随机推荐
- Linux-入门配置jdk,tomcat,mysql
Mysql安装 大家可以在这里下 http://mirrors.163.com/mysql/Downloads/MySQL-5.7/ 1)查看CentOS自带的mysql rpm -qa | grep ...
- oracle命令2
使用DDL创建和管理表 DBA角色:拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构,并且系统权限也需要DBA授出,且DBA用户可以操作全体用户的任意基表,包括删除 grant dba t ...
- LDAP学习总结
一.简介: LDAP(Light Directory Access Portocol),它是基于X.500标准的轻量级目录访问协议.目录是一个为查询.浏览和搜索而优化的数据库,它成树状结构组织数据,类 ...
- PCL数据类型和ROS数据类型的转换
参考网址 http://wiki.ros.org/pcl/Overview ,重点参看第2和第3节. 1. Data types 介绍了三种点云数据类型:sensor_msgs::PointCloud ...
- 阿里云轻量应用服务器 怎么控制怎么上传文件怎么安装JDK和Tomcat怎么完成JavaWeb的部署
你是否遇到过这些问题,自己的javaweb项目本地运行一切正常,但是一旦转移到阿里服务器之类的.就出现以下问题. 1 jsp无法解析java类 2 Only a type can be importe ...
- sas 获取数据集观测数量
DATA _NULL_;SET SASHELP.CLASS NOBS=N;CALL SYMPUTX('N',N);STOP;RUN; %PUT N=&N.; 运行结果: 57 DATA _NU ...
- 高性能mysql 第五章 索引部分总结
高性能索引 1.索引基础:索引的作用类似'目录'帮助Query来快速定位数据行. 1.1索引类型: 1.1.1 b-tree索引 b-tree(balance tree)索引:使用平衡树(非平衡二叉树 ...
- C# WinForm 实现窗体淡入淡出
有时候我们需要给窗体的打开和关闭添加点动画效果.最近正好有这类需求,于是研究了下窗体的淡入淡出,很简单就实现了,这里发表下成果,以供朋友们使用. 在Windows,有一个API,可以设置窗体的可见度, ...
- C语言数据结构基础学习笔记——栈和队列
之前我们学过了普通的线性表,接下来我们来了解一下两种特殊的线性表——栈和队列. 栈是只允许在一端进行插入或删除的线性表. 栈的顺序存储结构也叫作顺序栈,对于栈顶指针top,当栈为空栈时,top=-1: ...
- element-vue-koa2-mysql实现文件上传
友情提示:这篇博客不会详细说明搭建过程 阅读群体建议:第一次使用node或者koa2写文件上传或者下载,因为你不知道用fs的哪个方法,我也是从fs里试水试了一天,各种百度才搞出来的,特别学过java的 ...