LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/
题目:
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
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.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “makes”
, word2 = “coding”
, return 1.
Given word1 = "makes"
, word2 = "makes"
, return 3.
题解:
若是两个指针p1 和 p2相等时,不更新res.
Time Complexity: O(n). Space: O(1).
AC Java:
class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
if(words == null){
return -1;
} int ind1 = -1;
int ind2 = -1;
int res = words.length; for(int i = 0; i < words.length; i++){
if(words[i].equals(word1)){
ind1 = i;
if(ind2 != -1){
res = Math.min(res, ind1 - ind2);
}
} if(words[i].equals(word2)){
ind2 = i;
if(ind1 != -1 && ind1 < ind2){
res = Math.min(res, ind2 - ind1);
}
}
} return res;
}
}
LeetCode Shortest Word Distance III的更多相关文章
- [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 ...
- [LeetCode] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- [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 ...
- [LeetCode] 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 II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- 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 ...
- [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 ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
随机推荐
- ztree学习之异步加载节点(一)
ztreedemo.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- filter:alpha(opacity=100,style=1)
filter:alpha(opacity=100,style=1) 1.opacity属性:设置透明度,取值0至100之间的任意数值,100表示完全不透明: 2.style属性:设置渐变风格: 0表示 ...
- Powershell的远程管理
powershell有强大的远程管理功能,但是现在遇到个问题,我们之前的客户端操作系统都是默认安装的,没做默认设置,请问如何通过gpo将所有和远程有关的设置都搞定啊?到底要设置哪些个选项? 我的环 ...
- 【新产品发布】《GM1001 4~20mA 高精度电流采集模块》
一.主要特性 1.测量精度高达±0.01%FS±0.002mA: 2.采样电阻仅10欧姆(20mA时压降仅0.2V),对被测系统影响 微乎其微: 3.差分输入,可测量正反电流无需改动硬件,使用方便: ...
- 分布式架构高可用架构篇_08_MyCat在MySQL主从复制基础上实现读写分离
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- OC中属性及方法
1.声明式属性 a.实例变量 b.声明属性 自动生成setter/getter方法 .h ->@property 属性类型 属性名; .m ...
- 《Ant权威指南》笔记(一)
Ant的由来(序) James Duncan Davidson当年用纯Java开发Tomcat的时候,不仅想让它跨平台运行,还想要在不同的操作系统上都能够进行开发和构建.这种较大的项目的编译构建过程是 ...
- 2016.04.28,英语,《Vocabulary Builder》Unit 20
nom, comes from the Latin word for 'name'. nominee is 'named', [ˌnɑːmɪ'niː] n. 被提名的人, 名义人. binomial ...
- twitter storm源码走读之8 -- TridentTopology创建过程详解
欢迎转载,转载请注明出处,徽沪一郎. 从用户层面来看TridentTopology,有两个重要的概念一是Stream,另一个是作用于Stream上的各种Operation.在实现层面来看,无论是str ...
- VR制作的规格分析
因为UE4的演示资源更丰富一些,我这边把UE4的有代表性的演示都跑了一遍,同时也通过Rift确认效果,和里面的资源制作方式. 首先,UE4是基于物理渲染的引擎,大部分都是偏向图像真实的.使用的材质 ...