Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

这道题给了我们一个单词数组,又给定了两个单词,让求这两个单词之间的最小距离,限定了两个单词不同,而且都在数组中。博主最先想到的方法比较笨,是要用 HashMap 来做,建立每个单词和其所有出现位置数组的映射,但是后来想想,反正建立映射也要遍历一遍数组,还不如直接遍历一遍数组,直接把两个给定单词所有出现的位置分别存到两个数组里,然后再对两个数组进行两两比较更新结果,参见代码如下:

解法一:

class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
vector<int> idx1, idx2;
int res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) idx1.push_back(i);
else if (words[i] == word2) idx2.push_back(i);
}
for (int i = ; i < idx1.size(); ++i) {
for (int j = ; j < idx2.size(); ++j) {
res = min(res, abs(idx1[i] - idx2[j]));
}
}
return res;
}
};

上面的那种方法并不高效,其实需要遍历一次数组就可以了,用两个变量 p1,p2 初始化为 -1,然后遍历数组,遇到单词1,就将其位置存在 p1 里,若遇到单词2,就将其位置存在 p2 里,如果此时 p1, p2 都不为 -1 了,那么更新结果,参见代码如下:

解法二:

class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
int p1 = -, p2 = -, res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = i;
else if (words[i] == word2) p2 = i;
if (p1 != - && p2 != -) res = min(res, abs(p1 - p2));
}
return res;
}
};

下面这种方法只用一个辅助变量 idx,初始化为 -1,然后遍历数组,如果遇到等于两个单词中的任意一个的单词,再看 idx 是否为 -1,若不为 -1,且指向的单词和当前遍历到的单词不同,更新结果,参见代码如下:

解法三:

class Solution {
public:
int shortestDistance(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 != - && words[idx] != words[i]) {
res = min(res, i - idx);
}
idx = i;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/243

类似题目:

Shortest Word Distance II

Shortest Word Distance III

参考资料:

https://leetcode.com/problems/shortest-word-distance/

https://leetcode.com/problems/shortest-word-distance/discuss/66931/AC-Java-clean-solution

https://leetcode.com/problems/shortest-word-distance/discuss/66939/Java%3A-only-need-to-keep-one-index

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Shortest Word Distance 最短单词距离的更多相关文章

  1. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  2. [leetcode]243. Shortest Word Distance最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  3. [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 ...

  4. [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 ...

  5. 243. Shortest Word Distance 最短的单词index之差

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  6. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

  7. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

  8. LeetCode Shortest Word Distance

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

  9. [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 ...

随机推荐

  1. ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档

    原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...

  2. .NET 垃圾回收与内存泄漏

    > 前言相信大家一定听过,看过甚至遇到过内存泄漏.在 .NET 平台也一定知道有垃圾回收器,它可以让开发人员不必担心内存的释放问题,因为它会自定管理内存.但是在 .NET 平台下进行编程,绝对不 ...

  3. Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  4. Kafka消息时间戳(kafka message timestamp)

    最近碰到了消息时间戳的问题,于是花了一些功夫研究了一下,特此记录一下.   Kafka消息的时间戳 在消息中增加了一个时间戳字段和时间戳类型.目前支持的时间戳类型有两种: CreateTime 和 L ...

  5. 类型“System.Data.Linq.DataContext”在未被引用的程序集中定义。必须添加对程序集“System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。

    解决方法:添加System.Data.Linq.dll引用 http://www.cnblogs.com/m84641693/archive/2010/07/26/1785100.html http: ...

  6. tornado+sqlalchemy+celery,数据库连接消耗在哪里

    随着公司业务的发展,网站的日活数也逐渐增多,以前只需要考虑将所需要的功能实现就行了,当日活越来越大的时候,就需要考虑对服务器的资源使用消耗情况有一个清楚的认知.     最近老是发现数据库的连接数如果 ...

  7. ORACLE实现自定义序列号生成

    实际工作中,难免会遇到序列号生成问题,下面就是一个简单的序列号生成函数 (1)创建自定义序列号配置表如下: --自定义序列 create table S_AUTOCODE ( pk1 ) primar ...

  8. Warning: mysql_connect(): No such file or directory 解决方案总结(操作系统: Mac)

    说明: 本文主要内容参考: Mac下PHP连接MySQL报错"No such file or directory"的解决办法, 并进行个人补充 1. 运行环境: Mac OS X 10.11.4 (M ...

  9. atitit.http原理与概论attilax总结

    atitit.http原理与概论attilax总结 1. 图解HTTP 作者:[日]上野宣 著1 2. HTTP权威指南(国内首本HTTP及其相关核心Web技术权威著作)1 3. TCP/IP详解(中 ...

  10. ES6(let 和 const)

    一项新技术的出现肯定是为了解决一些问题,那么ES6的出现主要是解决了哪些问题?它的出现给我们带来了什么便利?当它没有出现的时候,某些问题怎么处理?ES6的方法和以前的方法比较又有什么不同呢?根据提出的 ...