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

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

  5. [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 ...

  6. [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 ...

  7. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

  8. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  9. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

随机推荐

  1. ASP。net中如何在一个按钮click事件中调用另一个按钮的click事件

    方法一: 直接指定 事件<asp:Button ID="btn1" runat="server" Text="按钮1" onclick ...

  2. bootstrap 下的 validation插件

    http://reactiveraven.github.io/jqBootstrapValidation/

  3. CI 笔记5 (CI3.0 默认控制器,多目录)

    在ci3.x中,不支持多级子目录的默认控制器设置, 解决方法如下: 在index.php中,添加  $routing['directory'] = 'admin';然后在默认的router.php的默 ...

  4. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

  5. zoj 3209.Treasure Map(DLX精确覆盖)

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

  6. C++ 语法规则

    C++ 中的布尔类型:布尔类型只占用一个bit ,但是如果连续定义多个布尔类型时,编译器可能会多个布尔类型定义在一起.true  编译器用1来表示.false  编译器用0来表示. 将一个其他类型的数 ...

  7. centos lnmp 安装笔记

    [root@host]# chkconfig nginx on [root@host]# service nginx start [root@host]# service nginx stop [ro ...

  8. Application.Exit()结束程序,但线程还在的解决方法

    转自:http://bbs.51cto.com/thread-970057-1.html 出现此情况大多原因是使用了多线程编程,或者你所调用的dll使用了多线程.  我们知道,一般情况下的线程执行完指 ...

  9. js手机站跳转

    var yunzhuanhua_pc_domain = "http://www.域名.com#yht"; //PC站网址var yunzhuanhua_wap_domain = & ...

  10. 局部变量存储区域静态变量存储区域static变量存储区域

    局部变量存储区域静态变量存储区域static变量存储区域 常见的存储区域可分为: 1.栈 由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 2.堆 ...