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. U盘被识别但不显示盘符怎么样才能解决?

    很多朋友在将U盘插入电脑后,会发现右下角的任务栏虽然出现了U盘的图标,但是在我的电脑中并没有显示出U盘的盘符,也就无法继续对U盘进行操作.遇到这种情况该怎么办呢?下面好系统U盘启动就告诉大家相应的解决 ...

  2. Makefile 编译静态库文件及链接静态库

    本文为原创文章,转载需指明该文链接 1.代码目录结构如下: comm/ comm/inc/apue.h  3 atexit.c Makefile  5 staticlib/lib/ staticlib ...

  3. block missing问题排查流程

    当集群出现block missing异常时,一般的排查流程如下: 首先检查是否有datanode处于dead或Decommissioned状态,如果是,尝试恢复datanode,一般block mis ...

  4. lib异步中断

    基于libusbx-1.0.18-rc1,libusbx现已重新merage到libusb.1. 初始化使用libusb_init初始化libusb,如果是单设备通信,ctx参数可以传NULL,表示使 ...

  5. Javac可以编译,Java显示找不到或无法加载主类

    运行时候加入完整包名.

  6. 题解 【POJ1187】 陨石的秘密

    解析 考虑到数据范围,其实我们可以用记搜. 设\(f[a][b][c][d]\)表示还剩\(a\)个'{}',\(b\)个"[]",\(c\)个"()",深度\ ...

  7. for 循环用了那么多次,但你真的了解它么?

    其实我们写代码的时候一直都在使用for循环,但是偶尔还是会纠结用哪一个循环. 一.基础的for循环 0.使用while也是一种循环方式,此处探究for相关的循环,就不做拓展了. 1.遍历数组的时候,初 ...

  8. java+大文件断点续传

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/1269085759/up6-jsp-mysq ...

  9. Confluence 6 上传一个附加文件的新版本

    有下面 2 种方法来上传一个附加文件的新版本,你可以: 上传与已有附件具有相同文件名的版本. 使用 上传一个新版本(Upload a new version)   按钮来进行上传(这个在文件预览界面中 ...

  10. sh_18_字符串文本对齐

    sh_18_字符串文本对齐 # 假设:以下内容是从网络上抓取的 # 要求:顺序并且居中对齐输出以下内容 poem = ["\t\n登鹳雀楼", "王之涣", & ...