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 ...
随机推荐
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
- Graph database_neo4j 底层存储结构分析(4)
3.3.2 DynamicStore 类型 3.3.2.1 AbstractDynamicStore 的存储格式 neo4j 中对于字符串等变长值的保存策略是用一组定长的 block ...
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
- EnableViewState=“false”不能乱用啊
有时候页面源文件里有一段看上去像乱码的代码,这时候为了加快页面的加载速度,可以使用EnableViewState=“false”,这时候页面上的乱码就会消失了.但是,关于这个问题作者郁闷了好久,之前为 ...
- 李洪强-C语言7-C语言运算符
C语言运算符 一.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. ①. 加法:+ 还可以表示正号 ②. 减法:- 还可以表示负号 ③. 乘法:* 非数学意义上的X ④. 除法:/ 注意 ...
- ThinkPHP添加模板时,犯的三个错
错误一:低级错误,将n打成看m,如图1 图1 这个找错,花了我将近2小时.扫了好几遍与之相关的代码,上网查了好些. 错误二:这个算是个低能的高级错误了.具体模板显示的效果如图2 图2 只要将相对地址及 ...
- Java如何获取系统cpu、内存、硬盘信息
1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...
- NBOJv2 1022 短信篮球(种类并查集)
Problem 1022: 短信篮球 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %lld ...
- Java学习资源
Java技术路线图 指路明灯 一位资深程序员大牛给予Java初学者的学习路线建议 Java源码阅读的真实体会 概要 JDK发展历程 Java项目经验 基于java平台的常用资源 官方文档 Java™ ...
- Linux常用命令(持续更新中)
cd 目录名 :进入某个目录 ls :列出当前目录的内容 locate 文件名/目录名:寻找文件.目录 find 目录名1 -name 文件名/目录名2 :在目录1中寻找目录2 whereis 文件 ...