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 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.
题目标签:Array
题目给了我们一个words array, word1 和word2, 让我们找到word1 和word2 之间最小距离。相比于 243. Shortest Word Distance, 这一题的区别就是,两个word 可能会相等。在243 code的基础上,稍微改一下就可以了。
因为如果当两个word 是相等的话,那么 不管出现的是word1 还是word2, 在比较words[i] 与 word1 的时候 都会是true,所以只要在这个情况里面 多加一个条件 -> 当 两个word 相等,并且 当前word 是至少第二个word1 的时候,记录它们的距离。
因为永远到不了else if, 所以p2 永远是 -1, 那么最后一个if 也永远不会进入。
当两个word 不相等的话,就和243题一样,具体看code。
一直想问,那么这一题的 之二 去哪里了? 怎么只有1 和3 呢?
Java Solution:
Runtime beats 46.30%
完成日期:09/08/2017
关键词:Array
关键点:当两个word相同时候,进入特别条件
class Solution
{
public int shortestWordDistance(String[] words, String word1, String word2)
{
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE;
boolean same = word1.equals(word2); for(int i=0; i<words.length; i++)
{
if(words[i].equals(word1)) // if find word1, mark its position
{
if(same && p1 != -1) // if two words are same and found a word before
min = Math.min(min, Math.abs(i - p1)); p1 = i;
}
else if(words[i].equals(word2)) // if find word2, mark its position
p2 = i; if(p1 != -1 && p2 != -1) // if find word1 and word2, save the smaller distance
min = Math.min(min, Math.abs(p1 - p2));
} return min;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 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] 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] 244. Shortest Word Distance II 最短单词距离 II
		
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
 - [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
		
Design a class which receives a list of words in the constructor, and implements a method that takes ...
 - LeetCode 243. 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 最短单词距离之二
		
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
 - 245. Shortest Word Distance III
		
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
 - 245. Shortest Word Distance III 单词可以重复的最短单词距离
		
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
 - 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
 
随机推荐
- Java I/O 从0到1 - 第Ⅰ滴血 File
			
前言 File 类的介绍主要会依据<Java 编程思想>以及官网API .相信大家在日常工作中,肯定会遇到文件流的读取等操作,但是在搜索过程中,并没有找到一个介绍的很简洁明了的文章.因此, ...
 - 06jQuery-03-选择器查找和过滤
			
1.查找 find().parent().prev().next() 通常情况下选择器可以直接定位到我们想要的元素,但是,当我们拿到一个jQuery对象后,还可以以这个对象为基准,进行查找和过滤. 最 ...
 - Oracle存储过程经典入门
			
ok基本就这些介绍
 - Apache CXF入门
			
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了.CXF 继承了 Celtix 和 XFire 两大 ...
 - 为什么自学java的人99%都学不会?
			
在学习java这条路上,有一类自学的学员,总让我感慨良多.这类学员,往往每天表现非常勤奋的学习,但学会的人却很少.他们极期勤奋,那么努力的学,也很认真,为什么就是学不会java呢? 通过小橙子我的大量 ...
 - react基础(2)
			
react基础(1):介绍了如何创建项目,总结了JSX.组件.样式.事件.state.props.refs.map循环,另外还讲了mock数据和ajax 还是用 react基础1 里创建的项目继续写案 ...
 - Day4 装饰器——迭代器——生成器
			
一 装饰器 1.1 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多 ...
 - JS之脚本延迟
			
自从开了博客,我就一下班回来匆匆吃完饭门一关等一开电脑一打开匆匆的研究东西,以至于朋友们都怀疑我是不是都得了自闭症 其实因为我有恐惧心理怕自己的技术哪天跟不上社会了,说到技术我觉得技术不求越新越好,但 ...
 - Undefined symbols for architecture arm64: "_OBJC_CLASS_$_WKWebView", referenced from: objc-c
			
出现: Undefined symbols for architecture arm64: "_OBJC_CLASS_$_WKWebView", referenced from: ...
 - snmp之GenericAddress
			
GenericAddress 注册地址类型,而不是默认的,第一次调用解析(java.lang.String的)方法之前,设置系统属性ADDRESS_TYPES_PROPERTIES. 这个类涉及到了工 ...