题目:

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. WPF学习01:初始XAML浅析

    本文内容: 浅析WPF应用默认创建的XAML中元素.attributes. 新建WPF工程“HelloWPF”. 初始创建的主窗体XAML代码如下: <Window x:Class=" ...

  2. java中封装

    .什么是封装? 封装就是将属性私有化,提供公有的方法访问私有属性. 做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(getter)方法和赋值(setter)方法,用于对这些属性 ...

  3. Entity Framework(一) 映射

    ADO.NET Entity Framework通过Modeel First和DataBase First,提供了几个把数据库表映射到对象上的曾.通过Database First,可以从一个数据库架构 ...

  4. 语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient)

    语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient) 原理 梅尔频率倒谱系数:一定程度上模拟了人耳对语音的处理特点 预加重:在语音信号中,高频部分的能 ...

  5. Build Settings

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

  6. iTween基础之Value(数值过度)

    一.基础介绍:二.基础属性 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50550527 一.基础介绍 Value有一个函数 Valu ...

  7. MITK Tutorial(二)

    目标: 生成MITK 插件包括一个新用户交互的视图,并调用一些ITK filters. Step 1: How to create a new MITK Plugin 可以选择用Plugin Gene ...

  8. C# 生成二维码并且在中间加Logo

    今天做项目的时候有个在生成二维码并且在中间加入Logo的需求,动手试了几把,总感觉效果没有之前写的好,就翻出旧代码,果然还是熟悉的味道,生成一张效果图如下 左边是微信里面的,右边是我自己生成的 原理比 ...

  9. For和While在C和MATLAB中的区别——MATLAB的大坑

    For和while是常见的循环关键字,在许多语言中都是通用的.但是想必不是所有人,都被其中的区别困扰过,尤其是MATLAB“程序员”. x=[,,,,,,]; i=; while i<=leng ...

  10. jQuery基于ajax实现星星评论代码

    本文实例讲述了jQuery基于ajax实现星星评论代码.分享给大家供大家参考.具体如下: 这里使用jquery模仿点评网的星星评论功能,Ajax评论模块,鼠标点击星星即可评价,下边是分数,可以点击后给 ...