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. 提升Scrapy框架爬取数据效率的五种方式

    1.增加并发线程开启数量 settings配置文件中,修改CONCURRENT_REQUESTS = 100,默认为32,可适当增加: 2.降低日志级别 运行scrapy时会产生大量日志占用CPU,为 ...

  3. Eclispe造成的tomcat占用端口 无法启动 强制终止进程 转载

    很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat  或者去修改tomcat的端口号 ,其实这么做太麻 ...

  4. mysql数据库:mysql增删改、单表、多表及子查询

    一.数据增删改 二.单表查询 三.正表达式匹配 四.多表查询 五.子查询       一..数据增删改     增加  insert [into] 表名[(可选字段名)] values(一堆值1),( ...

  5. 关于C语言打印string类字符串的问题

    首先因为printf函数输出字符串是针对char *的,即printf只能输出c语言的内置数据,而string不是c语言的内置数据. 其次string类型的对象不止包含字符串,还包含了许多用于操作的函 ...

  6. imx6ull增加qt5 qtserialbus库

    meta-qt5库地址:https://code.qt.io/cgit/yocto/meta-qt5.git/   1.在fsl-release-yocto/sources/meta-qt5/reci ...

  7. DP tricks and experiences

    [LeetCode] 关于动态规划的经验与技巧. 很多时候多分配一位空间是为了递推的方便,例如前推两位. 辅助数组的索引,用到的可能是 1 — N,或者是 0 — N-1. 具体要看清 f[i] 每一 ...

  8. Linux下mount存储盘遇到的错误

    一.注意点 1.超过1T的盘,创建的分区要查看是否初始化为了GPT格式. 2.如果新添加的盘是从存储上挂载的,涉及到多路径的问题,挂载的是多路径的盘符,比如:/dev/mapper/mpatha(对应 ...

  9. 前端Web浏览器基于Flash如何实时播放监控视频画面(二)之Windows搭建(RTMP)流媒体服务器

    本片文章只是起到抛砖引玉的作用,能从头到尾走通就行,并不做深入研究.为了让文章通俗易懂,尽量使用白话描述. 0x001: 获取 流媒体服务器有很多,这里以nginx为例. nginx for Wind ...

  10. JVM(六),java内存模型

    六.java内存模型 1.线程独占部分 (1)程序计数器 (2)Java虚拟机栈 (3)本地方法栈 (4)递归为什么会引发java.lang.StackOverFlowError异常吗 2.线程共享部 ...