[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 ...
随机推荐
- vs中debug和release版本的区别(转)
vs中的程序有debug和release两个版本,Debug通常称为调试版本,通过一系列编译选项的配合,编译的结果通常包含调试信息,而且不做任何优化,以为开发 人员提供强大的应用程序调试能力.而Rel ...
- LayoutInflater类详解
http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于 ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- JSP JS 日期控件的下载、使用及注意事项
网上流行的时间日期控件比较多,个人觉得My97DatePicker的日期控件不错,值得推荐. 具体的使用过程如下: 1.下载My97DatePicker.rar或 My97DatePickerBeta ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 《chkconfig命令》-linux命令五分钟系列之四
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- 利用C#的反射机制动态调用DLL类库
最近由于业务要求,需要动态调用DLL类库,所以研究了一下,感觉还好也不太难,今天就把自己理解的写了一个小例子(已经通过VS2005跑通),供大家一起研究和探讨,有理解不当的地方还请高手们多多指正,谢谢 ...
- Bootstrap_Javascript_弹窗
一. 结构分析 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在“modal-content ...
- Sublime text3 JS语法检测工具安装及使用
Sublime text3 JS语法检测工具安装及使用 工具/原料 sublime text3 nodejs sublimeLinter sublimeLinter-jshint 方法/步骤 首先ct ...
- yii框架的foreach 已经优化好了,可以“$user_model->attributes=$_POST['Admin'];”
yii框架的foreach 已经优化好了, 以前我们遍历数组的时候是用foreach循环 foreach ( as $key=>$value){ $user ...