题目:

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. Ajax入门小例子

    大牛文章:http://www.cnblogs.com/guduoduo/p/3681296.html                               ---Ajax基础学习 http:/ ...

  2. python学习第七天

    一.        subprocess 模块 1. subprocess的介绍:用来替代几个老的模块或是函数,如:os.systam,os.popen,os.spawn*,os.popen2*,co ...

  3. synchronized 用法,实例讲解

    package com.asiainfolinkage.ems.web.controller.base; import java.math.BigInteger; import java.util.D ...

  4. hadoop的核心思想

    hadoop的核心思想 1.1.1. hadoop的核心思想 Hadoop包括两大核心,分布式存储系统和分布式计算系统. 1.1.1.1. 分布式存储 为什么数据需要存储在分布式的系统中哪,难道单一的 ...

  5. OPatch failed with error code 73

    前几天给一套LINUX下的RAC数据库打补丁升级,有一台机器更新失败了,相关的异常内容如下: Restoring "/u01/app/oracle/11.2.0/db_1" to ...

  6. wap_supplicant介绍

    目前可以使用wireless-tools 或wpa_supplicant工具来配置无线网络.请记住重要的一点是,对无线网络的配置是全局性的,而非针对具体的接口. wpa_supplicant是一个较好 ...

  7. macos port总结

    http://stackoverflow.com/questions/733951/unable-to-download-the-source-code-of-open-source-projects ...

  8. VBS基础篇 - Err对象

    Err对象是一个具有全局范围的内部对象,含有关于错误的所有信息.On Error Resume next 忽略运行时产生的所有错误On Error Goto 0 取消忽略错误措施主要方法有:Clear ...

  9. Android-Empty-Layout:展示不同类型的页面布局,用于视图是空的时候

    Android-Empty-Layout:这个布局可以作用在Listview,Gridview,用于显示数据的是空的时候,可以提示友好的页面.这库可以显示页面出错,页面加载,页面是空. 加载的动画页面 ...

  10. 谈谈Android系统启动时的那点事儿

    Android系统完整的启动过程,从系统层次角度可分为Linux系统层.Android系统服务层.Zygote进程模型三个阶段:从开机到启动Home Launcher完成具体的任务细节可分为七个步骤, ...