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. 关于DOM事件流、DOM0级事件与DOM2级事件

    一.DOM 事件模型 DOM 事件模型包括捕获和冒泡,捕获是从上往下到达目标元素,冒泡是从当前元素,也就是目标元素往上到 window 二.流 流的概念,在现今的 JavaScript 中随处可见.比 ...

  2. [转]Linux-虚拟网络设备-tun/tap

    转: 原文:https://blog.csdn.net/sld880311/article/details/77854651 ------------------------------------- ...

  3. 使用Simian进行重复代码检测

    一.概述 Simian是一个可跨平台使用的重复代码检测工具,有商用和免费两种使用渠道,官方网址为:http://www.harukizaemon.com/simian/installation.htm ...

  4. 三.Python变量,常量,注释

    1. 运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确定后 写入代码python d:t1.py 您已经 ...

  5. 使用for循环签到嵌套制作直角三角形

    注意代码的运行顺序: for(i = 0 ; i<9 ; i++){ for(j = 0 ; j<i-1 ; j++){ document.write("*")//** ...

  6. 洛谷 P4017 最大食物链计数 题解

    P4017 最大食物链计数 题目背景 你知道食物链吗?Delia生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条.于是她来就来求助你,然而你也不会啊!写一个程序来帮帮她吧 ...

  7. 洛谷 P2085 最小函数值

    目录 题目 思路 \(Code\) 题目 戳 思路 首先这些函数全部单带递增,因为\(a\),\(b\),\(c\)都是正整数. 我们将全部的函数的\(x\)为\(1\)时的函数值放入优先度列(小根堆 ...

  8. 获取页面scroll高度

    记录一下获取 scroll 高度的方法 经实际测试: document.body.scrollTop 在 chrome 下会返回0. 所以 document.documentElement.scrol ...

  9. python 获取 一个正整数的二进制

    #coding=utf- def getbin(a): out = "" # 辗转相除法 ): div = a mod = a % out += str(mod) ): break ...

  10. rabbitmq添加user及vhost

    rabbitmqctl add_vhost /myhost # 添加 vhost rabbitmqctl add_user me me123 # 设置用户和密码 rabbitmqctl set_per ...