[LeetCode] 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
Note:
You may assume word1 and word2 are both in the list.
这道题还是让我们求最短单词距离,有了之前两道题 Shortest Word Distance II 和 Shortest Word Distance 的基础,就大大降低了题目本身的难度。这里增加了一个条件,就是说两个单词可能会相同,所以在第一题中的解法的基础上做一些修改,博主最先想的解法是基于第一题中的解法二,由于会有相同的单词的情况,那么 p1 和 p2 就会相同,这样结果就会变成0,显然不对,所以要对 word1 和 word2 是否的相等的情况分开处理,如果相等了,由于 p1 和 p2 会相同,所以需要一个变量t来记录上一个位置,这样如果t不为 -1,且和当前的 p1 不同,可以更新结果,如果 word1 和 word2 不等,那么还是按原来的方法做,参见代码如下:
解法一:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int p1 = -, p2 = -, res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
int t = p1;
if (words[i] == word1) p1 = i;
if (words[i] == word2) p2 = i;
if (p1 != - && p2 != -) {
if (word1 == word2 && t != - && t != p1) {
res = min(res, abs(t - p1));
} else if (p1 != p2) {
res = min(res, abs(p1 - p2));
}
}
}
return res;
}
};
上述代码其实可以优化一下,我们并不需要变量t来记录上一个位置,将 p1 初始化为数组长度,p2 初始化为数组长度的相反数,然后当 word1 和 word2 相等的情况,用 p1 来保存 p2 的结果,p2 赋为当前的位置i,这样就可以更新结果了,如果 word1 和 word2 不相等,则还跟原来的做法一样,这种思路真是挺巧妙的,参见代码如下:
解法二:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
if (words[i] == word2) p2 = i;
res = min(res, abs(p1 - p2));
}
return res;
}
};
我们再来看一种更进一步优化的方法,只用一个变量 idx,这个 idx 的作用就相当于记录上一次的位置,当前 idx 不等 -1 时,说明当前i和 idx 不同,然后在 word1 和 word2 相同或者 words[i] 和 words[idx] 相同的情况下更新结果,最后别忘了将 idx 赋为i,参见代码如下;
解法三:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int idx = -, res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1 || words[i] == word2) {
if (idx != - && (word1 == word2 || words[i] != words[idx])) {
res = min(res, i - idx);
}
idx = i;
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/245
类似题目:
参考资料:
https://leetcode.com/problems/shortest-word-distance-iii/
https://leetcode.com/problems/shortest-word-distance-iii/discuss/67097/12-16-lines-Java-C%2B%2B
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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 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 最短单词距离 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 III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [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 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
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
随机推荐
- linux终端指令总结
一直没机会进行linux指令的系统学习,但是工作中总能遇到通过指令操作文件或数据库的情况,总不能一味地依赖后端开发者的帮忙.上任领导说过,要是在同一个地方跌倒,那么你就是傻子.我可不想成为傻子,so, ...
- base的应用
------------父类 public class Person { public Person(string name,int age) { this.Na ...
- ASP.NET WebApi 文档Swagger中度优化
本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 写在前面 在后台接口开发中,接口文档是必不可少的.在复杂的业务当中和多人对接的情况下,简 ...
- 前端渲染利器——JsRender入门
JsRender不少前端人员应该都用过,它是一个比较强大的模板,不牵涉太多技术依赖,使用起来非常舒服.我本人在前端开发中使用React之前,都是用的它了(实际上我感觉React没有JsViewes好用 ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
- 初识IOS,Label控件的应用。
初识IOS,Label控件的应用. // // ViewController.m // Gua.test // // Created by 郭美男 on 16/5/31. // Copyright © ...
- 实现下来ScrollView放大轮播图
创建工程,创建一个UIScrollView属性,并遵循其协议: #define kWidth self.view.frame.size.width//屏幕宽 #define kHeight self. ...
- iOS UIApplication sharedapplication用法
应用中打开其他应用 我们来讨论一下,在iOS开发中,如何实现从app1打开app2. 基本的思路就是,可以为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中,可以传送一 ...
- (十二)Maven生命周期和插件
除了坐标.依赖以及仓库之外,Maven的另外两个核心概念是生命周期和插件.在有关Maven的日常使用中,命令行的输入往往就对应了生命周期,如mvn package就表示执行默认生命周期阶段packag ...
- ASP.NET MVC 让@Html.DropDownList显示默认值
在使用@Html.DropDownList的过程中,发现它的用法很局限,比如在加载的时候显示设定的默认项或者调整它的显示样式,在网上查了一些资料,终于把这个问题解决了. 一.View代码 @using ...