题目:

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. 修改UI中的值,无反应解决办法

    var targetObj = $("<input name='mydate' class='easyui-datebox'>").appendTo("#id ...

  2. Oracle 10g RAC 启动与关闭

    一. 检查共享设备 一般情况下,存放OCR和Voting Disk的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了. 1.1 如果使用ocfs2的 检查ocfs2 ...

  3. Hive表分区

    必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...

  4. access_ok()

    access_ok() 函数是用来代替老版本的 verify_area() 函数的.它的作用也是检查用户空间指针是否可用. 函数原型: access_ok (type, addr, size); 变量 ...

  5. php 彩票类 lottery

    <?php /* * For the full copyright and license information, please view the LICENSE * file that wa ...

  6. 二、有限状态机(FSM)

    1.状态机的作用?是什么? 状态机,顾名思义就是用来描述状态的.完善一点就是在同一的时钟下.更准确说是一种verilogHDL编程思想. 例如我们每一个系统都可以分为好几种状态,如:开始,初始化,运行 ...

  7. 高效开发Android App的10个建议(转)

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗 电或者内存占用 ...

  8. VBS基础篇 - 运算符

    VBScript 有一套完整的运算符,包括算术运算符.比较运算符.连接运算符和逻辑运算符. 运算符优先级: 首先计算算术运算符,然后计算比较运算符,最后计算逻辑运算符. 所有比较运算符的优先级相同,即 ...

  9. oracle中操作数据

    使用特定格式插入日期值 insert into emp values (,', to_date('1988-11-11','yyyy-mm-dd'), ); ,); 使用子查询插入数据 create ...

  10. Daily Scrum 11.9

    摘要:本次的meeting主要是继续讨论程序的问题以及单元测试和集成测试等,本次为1.01版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Tas ...