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

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “coding”word2 = “practice”, return 3.
Given word1 = "makes"word2 = "coding", return 1.

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

给一个单词数组和两个单词,返回这两个单词在数组里的最短距离。假定两个单词不同,而且都在数组中。

Java:

public int shortestDistance(String[] words, String word1, String word2) {
int m=-1;
int n=-1; int min = Integer.MAX_VALUE; for(int i=0; i<words.length; i++){
String s = words[i];
if(word1.equals(s)){
m = i;
if(n!=-1)
min = Math.min(min, m-n);
}else if(word2.equals(s)){
n = i;
if(m!=-1)
min = Math.min(min, n-m);
}
} return min;
}  

Python:

# Time:  O(n)
# Space: O(1) class Solution:
# @param {string[]} words
# @param {string} word1
# @param {string} word2
# @return {integer}
def shortestDistance(self, words, word1, word2):
dist = float("inf")
i, index1, index2 = 0, None, None
while i < len(words):
if words[i] == word1:
index1 = i
elif words[i] == word2:
index2 = i if index1 is not None and index2 is not None:
dist = min(dist, abs(index1 - index2))
i += 1 return dist

C++:

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

C++:

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

类似题目:

[LeetCode] 244. Shortest Word Distance II 最短单词距离 II

[LeetCode] 245. Shortest Word Distance III 最短单词距离 III

All LeetCode Questions List 题目汇总

[LeetCode] 243. 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] Shortest Word Distance 最短单词距离

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

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

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

  4. LeetCode 243. Shortest Word Distance (最短单词距离)$

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

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

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

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

  8. 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 ...

  9. [LeetCode#244] Shortest Word Distance II

    Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...

随机推荐

  1. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

  2. mysql技巧一则-避免重复插入相同数据

    今天解决的问题如下: 如果避免插入或更新一条数据表中相同名称的记录? , ,, , , '2019-06-18 07:20:48', '2016-06-18 07:20:48', 'manaual r ...

  3. destoon下动态链接301到伪静态(ngnix)

    分享一个destoon6.0/7.0下动态链接301到伪静态上面,实现权重提升. if ($request_uri ~* "^/index.php\?itemid=(\d+)&mod ...

  4. test20190904 JKlover

    100+100+100=300.最后十分钟极限翻盘. 树链剖分 给一棵以1为根的有根树,开始只有1有标记. 每次操作可以给某个点打上标记,或者询问从某个点开始向上跳,遇到的第一个有标记的点. 对于 1 ...

  5. HTML5 自定义属性 data-*属性名一定要小写吗?

    最近学习 javascript ,参考书籍是< javascript 高级程序设计>第三版,在介绍自定义元素属性时书中给出了一个例子,如下:<div id="myDiv&q ...

  6. django-文件上传和下载--fastDFS安装和配置

    5.1 安装fastdfs依赖包 一:下载安装FDFS依赖: libfastcommon 下载地址:https://codeload.github.com/happyfish100/libfastco ...

  7. SOA与ESB,微服务与API网关

    SOA与ESB,微服务与API网关 SOA: ESB: 微服务: API网关: 参考资料: 1.漫画微服务,http://www.sohu.com/a/221400925_100039689 2.SO ...

  8. 微信小程序——获取当天的前一个月至后一个月

    看标题也不知道你有没有明白我想表达的意思,先上个动态图吧~ 需要分析: 1.获取当前日期的前一个月,后一个月和当月.比如说现在是7月5号,我需要得到6月5号至8月5号的日期,同时还要返回当前的星期. ...

  9. 纯js房贷计算器开源

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. asp.net大附件上传,支持断点续传

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...