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 ...
随机推荐
- 第十章· Logstash深入-Logstash与Redis那点事
Logstash将日志写入Redis 为什么要使用Redis 在企业中,日志规模的量级远远超出我们的想象,这就是为什么会有一家公司日志易专门做日志收集,给大型金融公司收集日志,比如银行,因为你有可能看 ...
- hadoop--大数据生态圈中最基础、最重要的组件
hadoop是什么? hadoop是一个由Apache基金会所开发的分布式系统基础架构,hdfs分布式文件存储.MapReduce并行计算.主要是用来解决海量数据的存储和海量数据的分析计算问题,这是狭 ...
- IO[File_API学习]
IO流[File_API学习的使用] File_API学习的使用1.名称分隔符 / \ separatorjava下路径:\ 在Windows下的路径,在java里 \ 是转义字符.需要 \\Str ...
- AD软件中可视栅格 捕捉栅格 电气栅格的功能和设置详解
AD16的栅格设置 AD16系统共有3种栅格:可视栅格.电气栅格.捕捉栅格. Snap:捕获栅格,如果设定值是10mil,鼠标的光标拖动零件引脚,距离可视栅格在10mil范围之内时,零件引脚自动的准确 ...
- java开发技巧
1,IDEA辅助功能Shift +F2去到有错误的地方Alt+Enter,会给出解决错误的建议: 2,调试,没问题的步骤,直接跳过,不要跳入细节: 调试时,要明确要跟踪的变量,不要陷入混乱: 3,调试 ...
- 网络协议相关面试问题-TCP与IP网络模型
互联网应用的实现主要是通过分层来实现的,每一层有自己相应的功能,上层依赖于下层,具体层次如下图: 下面具体一层层来了解: 物理层 / 实体层: 也就是将电脑通过物理的手段连接起来,其实也就是01电子信 ...
- Lambda方法推导(method references)
在上一篇[http://www.cnblogs.com/webor2006/p/7707281.html]中提到了方法推导的东东: 这里说细的学习一下它,下面走起! Method references ...
- javaWeb中的session和cookie
Cookie Cookie 是浏览器提供的一种技术,通过服务器的程序能将一些只须保存在客户端,或者 在客户端进行处理的数据,放在本地的计算机上,不需要通过网络传输,因而提高网页处理的效率,并且能够减少 ...
- 吐槽一下jsoup
网络爬虫的本质就是通过域名加上特定的路由方式与远程资源建立一个短暂的连接,然后通过io流的方式读取.然后说一下jsoup,jsoup可以说是目前的爬虫工具包里面对java底层的工具类封装最简单的一种了 ...
- @Mapper和@Repository
参考地址 https://www.cnblogs.com/jtfr/p/10962205.html 相同点 两者都是作用于dao上 不同点 @Repository需要在Spring中配置扫描地址,然后 ...