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.


题目标签:Array

  题目给了我们一个words array, word1 和word2, 让我们找到word1 和word2 之间最小距离。相比于 243. Shortest Word Distance, 这一题的区别就是,两个word 可能会相等。在243 code的基础上,稍微改一下就可以了。

  因为如果当两个word 是相等的话,那么 不管出现的是word1 还是word2,  在比较words[i] 与 word1 的时候 都会是true,所以只要在这个情况里面 多加一个条件 -> 当 两个word 相等,并且 当前word 是至少第二个word1 的时候,记录它们的距离。

  因为永远到不了else if, 所以p2 永远是 -1, 那么最后一个if 也永远不会进入。

  当两个word 不相等的话,就和243题一样,具体看code。

  一直想问,那么这一题的 之二 去哪里了?  怎么只有1 和3 呢?

Java Solution:

Runtime beats 46.30%

完成日期:09/08/2017

关键词:Array

关键点:当两个word相同时候,进入特别条件

 class Solution
{
public int shortestWordDistance(String[] words, String word1, String word2)
{
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE;
boolean same = word1.equals(word2); for(int i=0; i<words.length; i++)
{
if(words[i].equals(word1)) // if find word1, mark its position
{
if(same && p1 != -1) // if two words are same and found a word before
min = Math.min(min, Math.abs(i - p1)); p1 = i;
}
else if(words[i].equals(word2)) // if find word2, mark its position
p2 = i; if(p1 != -1 && p2 != -1) // if find word1 and word2, save the smaller distance
min = Math.min(min, Math.abs(p1 - p2));
} return min;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 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] 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. [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 ...

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

  5. LeetCode 243. Shortest Word Distance (最短单词距离)$

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

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

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

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

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

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

随机推荐

  1. python基础之字典、赋值补充

    字典常用操作: 存/取info_dic={'name':'egon','age':18,'sex':'male'} print(info_dic['name11111111']) print(info ...

  2. jmeter按比例执行业务场景

    可用函数 __counter实现: 函数助手中 找到 __counter,如 ${__counter(false,num)},功能简介 ---- 参数为true,每个用户有自己的计数器 ---- 参数 ...

  3. Ubuntu16.04.1上搭建分布式的Redis集群

    为什么要集群: 通常为了,提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis是一个很好的Cache工具,当然了还有Memcached,这里只讲Redis.在我们的电 ...

  4. TComboBox组件重要属性和事件

    TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写 DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数 ...

  5. QT_FORWARD_DECLARE_CLASS

    相当于class 类名. 那么他和#include 包含头文件有什么区别呢 首先我们为什么要包括头文件问题的回答很简单通常是我们需要获得某个类型的定义(definition).那么接下来的问题 ...

  6. 模糊搜索神器fzf

    前言 fzf是目前最快的fuzzy finder.使用golang编写.结合其他工具(比如ag和fasd)可以完成非常多的工作. 让你通过输入模糊的关键词就可以定位文件或文件夹.当你的思维也习惯了模糊 ...

  7. 一个“.java”文件中是否可以包含多个类(不是内部类)?有什么限制?

    可以,若这个类的修饰符是public则,其类名须与文件名相同.

  8. GCD SUM 强大的数论,容斥定理

    GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...

  9. 原创:工作指派问题解决方案---模拟退火算法C实现

    本文忽略了对于模拟退火的算法的理论讲解,读者可参考相关的博文或者其他相关资料,本文着重于算法的实现: /************************************************ ...

  10. wireshark数据包分析实战 第二章

    1,监听网络线路:即嗅探器的位置确定. 2,混杂模式:将网卡设置成混杂模式,网卡可以接受经过网卡的所有数据报,包括目的地址不是本网卡的数据报.这些数据都会发送给cpu处理,这样,wireshark就能 ...