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. java对象与xml相互转换 ---- xstream

    XStream是一个Java对象和XML相互转换的工具,很好很强大.提供了所有的基础类型.数组.集合等类型直接转换的支持. XStream中的核心类就是XStream类,一般来说,熟悉这个类基本就够用 ...

  2. 在window系统下配置login.sql

    在window系统下配置login.sql 他的位置是登录用户的文件夹,我的win7系统位置是: C:\Users\Administrator 我的login.sql下载地址: http://file ...

  3. C# 异步操作

    在程序中,普通的方法是单线程的.但中途如果有大型的操作,比如读取大文件,大批量操作数据库,网络传输等,都会导致程序阻塞,表现在界面上就是程序卡或者死掉,界面元素不动了,不响应了.C#异步调用很好的解决 ...

  4. Builder 模式

    Builder 模式和 AbstractFactory 模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的 ...

  5. SGU 121.Bridges painting

    原题地址 题意: 新百慕大由N个岛屿组成,在岛屿之间有一些连接它们的桥.在任意两个岛屿之间,最多只有一座桥连接它们.总统先生下达命令,要求给所有桥上色. 每一座桥能被染成 白色 或者 黑色. 每一个岛 ...

  6. Struts2中使用execAndWait后,在 Action中调用getXXX()方法报告java.lang.NullPointerException异常的原因和解决方法

    使用 Struts2 编写页面,遇到一个要长时间运行的接口,因此增加了一个execAndWait ,结果在 Action 中调用 getContext()的时候报告异常 ActionContext c ...

  7. mysql锁死的现象判断

    一般发生表锁死这种低级问题,就有两种情况:1.程序员水平太菜,2.程序逻辑错误. 一旦发生系统会出现超时,关键是有可能你看不到正在活动的php进程,而系统的慢查询日志也不会记录,只能通过show fu ...

  8. MySQL 5.6 for Windows 解压缩版配置安装(转)

    转自:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给 ...

  9. 用Python高亮org-mode代码块

    文章同时可在我的github blog上阅读:http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.h ...

  10. BIOS中断大全

    BIOS中断大全 BIOS中断:1.显示服务(Video Service——INT 10H)  00H —设置显示器模式0CH —写图形象素01H —设置光标形状0DH —读图形象素02H —设置光标 ...