题目:

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.

链接: http://leetcode.com/problemset/algorithms/

题解:

找两个单词在数组中的距离。这个题比较没有意义...就跟找两个字母的距离一样。乍看题目还以为是word ladder。设置两个变量,分别代表每个单词的lastIndex,之后就是判断和计算了。也可以用一个变量来记录。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if(words == null || words.length == 0 || word1 == null || word2 == null)
return Integer.MAX_VALUE;
int minDistance = Integer.MAX_VALUE;
int word1Index = -1, word2Index = -2; for(int i = 0; i < words.length; i++) {
if(words[i].equals(word1))
word1Index = i;
if(words[i].equals(word2))
word2Index = i;
if(word1Index >= 0 && word2Index >= 0)
minDistance = Math.min(minDistance, Math.abs(word1Index - word2Index));
} return minDistance;
}
}

二刷:

使用两个变量存下 word1和word2在数组中的位置,然后进行计算。

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if (words == null || words.length < 2 || word1 == null || word2 == null) {
return Integer.MAX_VALUE;
}
int word1Pos = -1, word2Pos = -1, minDistance = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
String curWord = words[i];
if (curWord.equals(word1) || curWord.equals(word2)) {
if (curWord.equals(word1)) {
word1Pos = i;
}
if (curWord.equals(word2)) {
word2Pos = i;
}
if (word1Pos >= 0 && word2Pos >= 0) {
minDistance = Math.min(minDistance, Math.abs(word1Pos - word2Pos));
}
}
}
return minDistance;
}
}

三刷:

Java:

public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int minDist = Integer.MAX_VALUE;
int word1Idx = -1, word2Idx = -1;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
word1Idx = i;
if (word2Idx >= 0) minDist = Math.min(minDist, i - word2Idx);
} else if (words[i].equals(word2)) {
word2Idx = i;
if (word1Idx >= 0) minDist = Math.min(minDist, i - word1Idx);
}
}
return minDist;
}
}

Reference:

243. Shortest Word Distance的更多相关文章

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

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

  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 243. Shortest Word Distance (最短单词距离)$

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

  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. [LC] 243. Shortest Word Distance

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

  6. 【LeetCode】243. Shortest Word Distance 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

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

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

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

随机推荐

  1. 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法

    有一种办法来提高Linux安全性--修改 hosts.allow , hosts.deny 这2个文件来配置 允许某个ip访问, 或者禁止访问. 可以通过这种方式设置限制 sshd 的远程访问, 只允 ...

  2. 使用Azure portal Create Virtual Machine

    使用简单快速的方式穿件的Virtual Machine 这个步骤隐藏的了很多步骤,例如的创建的云服务(Cloud Service) 创建存储(Storage) 存储名为系统自动产生 可以通过存储看到含 ...

  3. 记一次Surface Pro 2还原操作

    因为要做Azure的一个case,对自己的域环境下直接进行了捕获.结果导致机器直接crash. 重启后使用本地账号登陆后发现所有Win 8 的App都无法使用,包括进入设置中还原方式也无法使用. 可以 ...

  4. visuall assist x 破解方法

    visuall assist x 破解方法   试用期过了后,不管怎么装,或者是找网上的KEY都不行,所以试了一下的方法,成功了: 1.先下载Visual Assist X 10.6.1845.0 2 ...

  5. ASP.NET MVC NonActionAttribute使用说明

    默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法. 如果您的 controller 类包含公共方法,并且您不希望它成为操作方法,则必须用 NonActionAttrib ...

  6. iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):4 同步编码

    本文档描述Video Toolbox实现同步编码的办法. Video Toolbox在头文件描述了编码方式为异步,实际开发中也确实为异步. This function may be called as ...

  7. php多条件查询

    $sql)"; if(!empty($uid)) { $sql .=" and uid= ".$uid; } if(!empty($time1) && e ...

  8. maven学习心得整理

    maven的学习心得 已经接触了maven项目有一段时间了,开始时仅仅会使用,在使用中发现了它的强大和方便,于是决心研究一下: 首先,普及一下maven参数: -D:传入属性参数 -P:使用POM中指 ...

  9. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  10. Codeforces Round #358 (Div. 2) D. Alyona and Strings 字符串dp

    题目链接: 题目 D. Alyona and Strings time limit per test2 seconds memory limit per test256 megabytes input ...