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.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “makes”, word2 = “coding”
Output: 1
Input: word1 = "makes", word2 = "makes"
Output: 3

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

这题虽然可以沿用上一题的思路,但是可以有更简单的两个index的做法,最优解O(n),runtime 8ms。

class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int index = -;
int minval = words.size();
for(int i=; i<words.size(); i++){
if((index != -) && (words[i] == word1 || words[i] == word2)){
if((words[index] == word1 && words[i] == word2) ||
(words[index] == word2 && words[i] == word1)){
minval = min(minval, i - index);
}
}
if(words[i] == word1 || words[i] == word2)
index = i;
}
return minval;
}
};

LC 245. Shortest Word Distance III 【lock, medium】的更多相关文章

  1. LC 244. Shortest Word Distance II 【lock, Medium】

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

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

  3. 245. Shortest Word Distance III

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

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

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

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

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

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

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

  8. LeetCode Shortest Word Distance III

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

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

随机推荐

  1. Solved: XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX

    esxi 出现XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX 问题. 回避方法: 将高级设置-->Us ...

  2. collections:内建模块,提供额外的集合类

    介绍 collections里面包含了很多除了内置类型之外的数据类型,我们使用它们有时可以很方便的完成一系列操作 ChainMap:搜索多个字典 from collections import Cha ...

  3. selenium:能够模拟人类打开浏览器的爬虫利器

    介绍 selenium相当于是一个机器人,可以模拟人类登陆浏览器的行为,比如点击.填充数据.删除cookie等等.Chromedriver是一个驱动Chrome的程序,使用它才可以驱动浏览器,其实Ch ...

  4. DA_06_iptables 与 firewalld 防火墙

    8.1 防火墙管理工具 防火墙作为公网与内网之间的保护屏障,在保障数据的安全性方面起着至关重要的作用.主要功能都是依据策略对穿越防火墙自身的流量进行过滤.防火墙策略可以基于 流量的源目地址.端口号.协 ...

  5. netty-1.从一个最简单的例子开始

    (原) 第一篇,从一个最简单的例子开始 1.netty是干什么,怎么用,这里不作介绍,先从一个例子来了解它, netty 5.0以上的版本被废弃了,以下例子从4.1.10.Final版本开始. 2.一 ...

  6. impdp导入报错39002

    原文:https://www.cnblogs.com/huacw/p/3888807.html 1 create directory data_pump_dir as '\exphd\datapump ...

  7. cmd_memo

    1. bind host or ip:port #指定域名 curl -H 'Host:www.tsuiz.com' http://10.14.54.131:8080/check.do #指定ip和端 ...

  8. Hadoop-No.14之文件传输的特点

    文件传输特点 这是一种all-or-nothing批处理方法,所以如果文件传输过程中出现错误,则不会写入或读取任何数据.这种方法与Flume,Kafka之类的采集方法不同,后者提供一定程度的错误处理功 ...

  9. hdu 6078 Wavel Sequence

    题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6078 (2017 Multi-University Training Contest - Team ...

  10. 【计算机-虚拟wifi】Win7虚拟wifi设置

    虚拟Wifi,可以让电脑变成无线路由器,实现共享上网.   设置步骤 1.以管理员身份运行:cmd.exe   2.启动并设置虚拟网卡:   命令窗口中输入:  netsh wlan set host ...