题目:

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. 使用dxNavBar动态创建应用程序菜单

    一.如何动态创建dxNavBar内容: function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer; va ...

  2. 通过 Javacore 了解线程运行状况

    Javacore 是一个当前 JVM 运行状态的快照.通过对Javacore 的分析,可以了解在 JVM 中运行的应用程序的当前状态,比如是否“卡”在某一点上,或在某些代码上运行时间太长. Javac ...

  3. (转)使用getevent监听Android输入设备文件

    尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节Android事件分发完全解 ...

  4. Activity的Launch mode详解 singleTask正解

    Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...

  5. C++实现数字媒体三维图像渲染

    C++实现数字媒体三维图像渲染 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画物体对象的功能.并附带放大缩小,旋 ...

  6. Payment Terms 收付款条件和分期付款设置

    SAP Payment Terms 中文翻译为收付款条件,他的用途是应收和应付的财务凭证中账期的管理,顾名思义即手动录入和自动生成的财务文档多少天内冲销处理则为正常,否则为超期应收应付文档,它包含的内 ...

  7. 在SQL SErver中实现数组功能

    T-SQL象数组一样处理字符串.分割字符串    在日常的编程过程中,数组是要经常使用到的.在利用SQL对数据库进行操作时,有时就想在SQL使用数组,比如将1,2,3,4,5拆分成数组.可惜的是在T- ...

  8. C# File

    http://msdn.microsoft.com/zh-cn/library/system.io.file(v=vs.110).aspx using System; using System.IO; ...

  9. IntelliJ IDEA 15 部署Tomcat及创建一个简单的Web工程

    一.部署Tomcat 二.创建一个简单的Web工程 2.1创建一个新工程 创建一个新工程 设置JDK及选择Web Application (创建的是Web工程) 点击Next,选择工作空间,起个工程名 ...

  10. 【转】成为it精英,我奋斗7年

    转载地址:http://liangwang985.blog.163.com/blog/static/119549233201191394259491/ 这些日子 我一直在写一个实时操作系统内核,已有小 ...