题目:

This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

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

word1 and word2 may be the same and they represent two individual words in the list.

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “makes”word2 = “coding”, return 1.
Given word1 = "makes"word2 = "makes", return 3.

Note:
You may assume word1 and word2 are both in the list.

链接: http://leetcode.com/problems/shortest-word-distance-iii/

题解:

也是Shortest word distance的follow up。这回两数可以一样。 在遍历的时候我们可以多写一个分支。 或者更简化的话可以把两个分支合并起来。

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

public class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
if(words == null || words.length == 0 || word1 == null || word2 == null)
return 0;
int minDistance = Integer.MAX_VALUE, word1Index = -1, word2Index = -1;
boolean isWord1EqualsWord2 = word1.equals(word2); for(int i = 0; i < words.length; i++) {
if(isWord1EqualsWord2) {
if(words[i].equals(word1)) {
if(word1Index >= 0)
minDistance = Math.min(minDistance, i - word1Index);
word1Index = i;
}
} else {
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(word2Index - word1Index));
}
} return minDistance;
}
}

Reference:

https://leetcode.com/discuss/50360/my-concise-java-solution

https://leetcode.com/discuss/50715/12-16-lines-java-c

245. Shortest Word Distance III的更多相关文章

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

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

  3. 245. Shortest Word Distance III 单词可以重复的最短单词距离

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

  4. LC 245. Shortest Word Distance III 【lock, medium】

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

  5. 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)

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

  6. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  7. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

  8. [Swift]LeetCode245.最短单词距离 III $ 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] 243. Shortest Word Distance 最短单词距离

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

随机推荐

  1. hadoop自动安装的脚本与步骤

    最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...

  2. visuall assist x 破解方法

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

  3. Entity Framework(序)

    ADO.NET Entity Framework 是一个对象-关系的映射结构,它提供了ADO.NET的一个抽象,可基于引用的数据库获取对象模型.可以通过Entity Framework 使用不同的变成 ...

  4. Linux 配置本地yum源

    Linux 配置无网络状态利用yum安装软件 在有网络的情况下安装软件只需一条yum install xxx命令,例如安装gcc只需一条指令:yum install gcc  那么在没有网络的情况下该 ...

  5. Build Settings

    Add Open Scenes 选择一个关卡,使其处于打开状态,在菜单栏选择 File -> Build Settings 打开Build Settings窗口.选择 Add Open Scen ...

  6. Configure xterm Fonts and Colors for Your Eyeball

    https://wiki.mpich.org/mpich/index.php/Configure_xterm_Fonts_and_Colors_for_Your_Eyeball Screenshot ...

  7. Xcode8之后 XMPP 重定义问题 Redefinition of module 'dnssd'

    在升级Xcode到8之后,原来的关于XMPP的项目运行报错,错误信息为: Redefinition of module 'dnssd' 系统和XMPP框架同时用到了 'dnssd',大概就是错误的原因 ...

  8. 关于表 datatable的条件查询

    关于表 datatable的条件查询 从数据库中获得一个datatable dt .里面有很多的死的数据.然后,我就是要在这个表中来按条件查询,例如 dt中有个列叫"palte", ...

  9. android开发 PopupWindow 设置充满屏幕

    View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...

  10. python 获取文件大小,创建时间和访问时间

    # -*- coding: UTF8 -*- import timeimport datetime import os 1. '''把时间戳转化为时间: 1479264792 to 2016-11-1 ...