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. C#操作域用户ADHelper

    在C#中操作域用户,在项目中写的帮助类: using System; using System.Collections.Generic; using System.DirectoryServices; ...

  2. BZOJ1499: 瑰丽华尔兹(单调队列)

    pro: 给出一个n*m的地图,刚开始人在(x,y),每次给出一段区间(l,r,t),表示在时间[l,r]内,可以使人向4个方向(t)移动一格,或者不动.求最大可以移动多少格. sol: 考虑每一列( ...

  3. axure快速上手

    Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写.Axure RP是美国Axur ...

  4. forword动作

    forword动作   服务器内部跳转指令 语法为: <jsp:forword page = "目标页面"> 等同于:request.getRequestDispatc ...

  5. C#中ref和out的原理

    去年在CSDN上写的,现在把它搬过来. 一.引发问题 用了那么久的 ref 和 out ,你真的了解它们是如何使得实参与形参的值保持同步的吗? 二.研究前提 要研究这个问题,前提是要了解 C# 中方法 ...

  6. Evaluation of fast-convergence algorithm for ICA-based blind source separation of real convolutive mixture

    实际卷积混合情况下,基于ICA的盲源分离算法快速收敛性能评估[1]. 提出了一种新的盲源分离算法,该算法将独立分量分析ICA和波束形成BF相结合,通过优化算法来解决盲源分离的低收敛问题.该方法由以下三 ...

  7. 思科ASA基本配置

    ------------恢复内容开始------------ ASA基本配置 ciscoasa#show running-config        //讲解已作的默认配置 ciscoasa#conf ...

  8. 洛谷 P3376 【模板】网络最大流 题解

    今天学了网络最大流,EK 和 Dinic 主要就是运用搜索求增广路,Dinic 相当于 EK 的优化,先用bfs求每个点的层数,再用dfs寻找并更新那条路径上的值. EK 算法 #include< ...

  9. 又一款dump文件观察工具---MiniDumpView

    简介 MiniDumpView实用程序可用于显示minidump中数据流的内容.特别是,可以显示以下信息: 操作系统和CPU信息 进程信息(进程ID和时间) 模块列表(包含每个模块的详细信息) 线程列 ...

  10. 【JZOJ6226】【20190618】纳什均衡

    题目 一颗二叉树,每个点儿子个数为0 或 2 ,对每个叶子有一个权值\((c(u),d(u))\) 从根结点开始走,Alice 可以选择奇数层的走法,Bob 可以选择偶数层的走法,分别获得最后走到叶子 ...