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. 枚举,Enum,常规使用demo记录

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...

  2. 监听polygon变化

    Polygons are made of Paths which are just MVCArrays (in this case, they're a list of LatLng objects) ...

  3. app图标和启动页设置

    弄了一下午,终于把iOS中图标的设置和启动页的设置弄明白了.我想以后再也不会浑了. 进入正题: 一:apple 1).iPhone4s 3.5寸屏,也就是640*960,但在模拟器上正常用的是320* ...

  4. OSX安装nginx和rtmp模块(rtmp直播服务器搭建)

    1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  5. 请教如何实现UITextField值变化的实时监视

    上网搜索以后发现基本的处理方法大概有三种1.KVO方式[textField addObserver:self forKeyPath:@"text" options:0 contex ...

  6. IIS部署.NET项目的有关事项_2015.07.02

    今天在做项目中的关于发送邮件的一些功能.在部署服务的时候遇到了一些奇葩的问题,基本上是和IIS有关的问题. 首先,项目是基于.NET Framework4.0 版本的,由于本人用的是一台新配置好的机器 ...

  7. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  8. js四舍五入的bug和方法

    简单来说js使用原生toFixed(x)截取小数的时候会有误差,出现在比如var o = 0.3303;o.toFixed(3);//0.330 toFixed(x)本来也是一个获取四舍五入的截取方法 ...

  9. App的token机制

    这只是网上看来的后期可能还会修改. 理论版的描述如下: (1) 服务器接收到app发送的用户名和密码后,验证用户名和密码是否正确. 如果错误则返回错误信息. 如果验证正确,生成一个随机的不重复的tok ...

  10. NSNumber

    integerfloatc 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象.NSNumber ...