[Locked] Shortest Word Distance I & II & III
Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
代码:
int swd(vector<string> words, string word1, string word2) {
int last = , pos1 = -, pos2 = -, mindist = INT_MAX;
for(int i = ; i < words.size(); i++) {
if(words[i] == word1) {
pos1 = i;
if(last == )
mindist = min(mindist, i - pos2);
last = ;
}
if(words[i] == word2) {
pos2 = i;
if(last == )
mindist = min(mindist, i - pos1);
last = ;
}
}
return mindist;
}
Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
代码:
class Solution {
private:
unordered_multimap<string, int> hash;
public:
Solution(vector<string> words) {
for(int i = ; i < words.size(); i++)
hash.insert(make_pair(words[i], i));
}
int swd(string word1, string word2) {
auto range1 = hash.equal_range(word1), range2 = hash.equal_range(word2);
auto pos1 = range1.first, pos2 = range2.first;
int mindist = INT_MAX;
while(pos1 != range1.second && pos2 != range2.second){
mindist = min(mindist, abs(pos1->second - pos2->second));
pos1->second > pos2->second ? pos1++ : pos2++;
}
return mindist;
}
};
Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
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.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “makes”, word2 = “coding”, return 1.
Given word1 = "makes", word2 = "makes", return 3.
Note:
You may assume word1 and word2 are both in the list.
代码:
int swd(vector<string> words, string word1, string word2) {
int last = , pos1 = -, pos2 = -, mindist = INT_MAX;
if(word1 == word2) {
for(int i = ; i < words.size(); i++) {
if(words[i] == word1) {
if(pos1 != -)
mindist = min(mindist, i - pos1);
pos1 = i;
}
}
return mindist;
}
for(int i = ; i < words.size(); i++) {
if(words[i] == word1) {
pos1 = i;
if(last == )
mindist = min(mindist, i - pos2);
last = ;
}
if(words[i] == word2) {
pos2 = i;
if(last == )
mindist = min(mindist, i - pos1);
last = ;
}
}
return mindist;
}
[Locked] Shortest Word Distance I & II & III的更多相关文章
- [LeetCode] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- [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 II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [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 ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
随机推荐
- C# 如何创建接口以及使用接口的简单Demo(转载!)
//No:1 首先,我们要封装一个接口,接口中不要实现具体的方法(说白了这就是一个架子而已!) using System;using System.Collections.Generic;using ...
- angularjs-ngModel传值问题
js NiDialog.open({ windowClass: '', backdrop: 'static', keyboard: false, templateUrl: '/static/tpl/a ...
- php+支付宝整合
CREATE TABLE IF NOT EXISTS `alipay_order` ( `id` ) unsigned NOT NULL auto_increment, `orderid` ) NOT ...
- 后台线程,优先级,sleep,yield
1.后台线程,是指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可获取的部分.当所有非后台线程结束时,程序也就 终止了,同时会杀死进程中所有后台线程.main()是一个非后 ...
- C# 线程数
理论上,一个进程可用虚拟空间是2G,默认情况下,线程的栈的大小是1MB,所以理论上最多只能创建2048个线程,但是一般不会到这么大,因为主线程要占内存,可能还要多点.如果要创建多于2048的话,必须修 ...
- Linux 添加epel源
1.epel-release yum install epel-release 这样有些没办法通过yum 安装 可以这样安装(例如redis)
- vs2008 下编译jrtplib-3.9.0成功
jrtplib-3.9.0的编译,终于搞通了.网上搜集了很多资料,自己也调试了很久. 首先,jrtplib-3.9.0是什么不用多说吧,它是一个很牛的老外用C++写的一个开源的RTP协议库,用它可以进 ...
- printf 缓冲区问题
突然发现printf的问题,看了这个很有意思,学习一下 转自:http://blog.csdn.net/shanshanpt/article/details/7385649 昨天在做Linux实验的时 ...
- asp.net MVC 从其它项目复制过来的Area里面的Controllers文件读取不到
从其实项目复制过来的Controllers,在访问时显示不存在文件 检查一下对应的area里面的AreaRegistration文件的命名空间是否一致
- python之加密
import hashlib obj = hashlib.md5(bytes('adfasfasdfsfasf',encoding = 'utf-8')) obj.update(bytes('123' ...