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 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.
Note:
You may assume word1 and word2 are both in the list.
链接: http://leetcode.com/problems/shortest-word-distance-iii/
题解:
也是Shortest word distance的follow up。这回两数可以一样。 在遍历的时候我们可以多写一个分支。 或者更简化的话可以把两个分支合并起来。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
if(words == null || words.length == 0 || word1 == null || word2 == null)
return 0;
int minDistance = Integer.MAX_VALUE, word1Index = -1, word2Index = -1;
boolean isWord1EqualsWord2 = word1.equals(word2);
for(int i = 0; i < words.length; i++) {
if(isWord1EqualsWord2) {
if(words[i].equals(word1)) {
if(word1Index >= 0)
minDistance = Math.min(minDistance, i - word1Index);
word1Index = i;
}
} else {
if(words[i].equals(word1))
word1Index = i;
if(words[i].equals(word2))
word2Index = i;
if(word1Index >= 0 && word2Index >= 0)
minDistance = Math.min(minDistance, Math.abs(word2Index - word1Index));
}
}
return minDistance;
}
}
Reference:
https://leetcode.com/discuss/50360/my-concise-java-solution
https://leetcode.com/discuss/50715/12-16-lines-java-c
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 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- 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】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- [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 III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [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] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
随机推荐
- thinkpad t440p 解决无线网卡驱动
$ wget https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1239578/+attachment/4057550/+files/rtl_9 ...
- 博主教你制作类似9patch效果的iOS图片拉伸
下面张图片,本来是设计来做按钮背景的: button.png,尺寸为:24x60 现在我们把它用作为按钮背景,按钮尺寸是150x50: // 得到view的尺寸 CGSize viewSize = ...
- java 进制转化
public static void toBinary(int num){ trans(num,1,1); } public static void toHex(int num){ trans(num ...
- C语言标准库函数strcpy与strcmp的简单实现
//C语言标准库函数strcpy的一种简单实现. //返回值:目标串的地址. //对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL. //参数:des为目标字符串, ...
- 修改myeclipse的jsp模板
在myeclipse的安装目录下: C:\Users\Seeker\AppData\Local\MyEclipse Professional\plugins 找到com.genuitec.eclips ...
- Notes of the scrum meeting(10/28)
meeting time:4:00~6:00p.m.,October 28th,2013 meeting place:雕刻时光 attendees: 顾育豪 ...
- android 开发解密时出现pad block corrupted 错误
情景:在虚拟机上运行正常的,但是到我的真机上就解密失败,出现pad block corrupted ,据说是版本原因:我机器是小米3 最新版的android 4.2 出现问题的代码: privat ...
- Thinkphp中路由Url获取的使用方法
Thinkphp是一个体系较为完整的框架,很多地方比国外的框架功能都全,唯一不喜之处是性能,和传说中的.NET有点像. Thinkphp提供较全url处理体系,通过同一规则实现Url的路由和Url生成 ...
- Jqgrid使用
$('#mygrid').jqGrid('GridUnload'); //保留table元素 $('#mygrid').jqGrid('GridDestroy '); //相当于remove,移除 ...
- 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...