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 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】的更多相关文章
- 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 ...
- [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 ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 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 ...
- 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [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 ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [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 ...
随机推荐
- 使用HTML5 -Canvas追踪用户,Chrome隐身模式阵亡
中国的一些精准营销公司又要偷着乐了= =从之前追踪Cookie到后面追踪FlashCookie,某些商家总在永无止境的追踪用户行为甚至是隐私,将其转化为所谓的“商业价值”.我们被迫面临“世风日下.道德 ...
- libusb传输endpoint描述符
至于endpoint描述符,它是属于设置的,每个设置都会有endpoint描述符,也就是每个接口的设置都表示一种功能,既然是实现了功能,那就必须通过endpoint来传输数据,那到底是用到了几个end ...
- 201271050130-滕江南-《面向对象程序设计(java)》第十七周学习总结
201271050130-滕江南-<面向对象程序设计(java)>第十七周学习总结 博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.c ...
- 201871010104-陈园园《面向对象程序设计(java)》第十七周学习总结
201871010104-陈园园<面向对象程序设计(java)>第十七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- mybatis整合Spring编码
mybatis整合Spring的核心代码 spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> ...
- linux内核 进程管理
进程和线程 进程不单单包含可执行代码(代码段),好包含打开的文件,挂起的信号,处理器状态,虚拟内存地址等. 线程:从内核的角度来说,它并没有线程这个概念.Linux把所有线程都当做进程来实现.内核并没 ...
- BZOJ 1818: [Cqoi2010]内部白点 (BIT + 扫描线)
就是求多条线段的交点数,直接BIT+扫描线就行了. 注意不要算重最初存在的点. CODE #include<bits/stdc++.h> using namespace std; char ...
- [TypeScript] @OnChange for ngOnChanges
Take away from NGCONF talk. It is a good show case to how to use decorator. export interface SimpleC ...
- Python之signal模块的使用
常用的信号值如下: 信号值 事件 处理方式 SIGHUP 终止进程 终端线路挂断 SIGINT 终止进程 中断进程 SIGQUIT "建立CORE文件终止进程,并且生成core文件" ...
- Python 爬虫十六式 - 第六式:JQuery的假兄弟-pyquery
PyQuery:一个类似jquery的python库 学习一时爽,一直学习一直爽 Hello,大家好,我是 Connor,一个从无到有的技术小白.上一次我们说到了 BeautifulSoup 美味 ...