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.

 class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
int idx1 = -, idx2 = -, res = words.size();
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};

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 WordDistance {
private:
unordered_map<string, vector<int>> wordidx;
public:
WordDistance(vector<string>& words) {
int n = words.size();
for (int i = ; i < n; ++i) wordidx[words[i]].push_back(i);
} int shortest(string word1, string word2) {
vector<int> &idx1 = wordidx[word1];
vector<int> &idx2 = wordidx[word2];
int m = idx1.size(), n = idx2.size();
int res = INT_MAX, i = , j = ;
while (i < m && j < n) {
res = min(res, abs(idx1[i] - idx2[j]));
if (idx1[i] > idx2[j]) ++j;
else ++i;
}
return res;
}
}; // Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");

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.

 class Solution {
public:
int shortest(vector<string> &words, string word) {
int pre = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word) {
if (pre != -) res = min(res, i - pre);
pre = i;
}
}
return res;
}
int shortestWordDistance(vector<string>& words, string word1, string word2) {
if (word1 == word2) return shortest(words, word1);
int idx1 = -, idx2 = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};

[LeetCode] Shortest Word Distance I & II & III的更多相关文章

  1. [Locked] 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 Shortest Word Distance III

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

  6. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  7. LeetCode Shortest Word Distance

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

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

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

随机推荐

  1. Android为ViewPager添加切换动画——自己定义ViewPager

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/44224517 在上篇博客中,我写了一个使用属性动画为ViewPager加入切换动画 ...

  2. java的配置方式简介

    1,java的配置方式简介java的配置方式是为了代替使用xml配置方式,主要使用两个注解:@Configuration//通过该注解来表明该类是一个spring的配置,相当于一个xml文件@Comp ...

  3. Region使用全解

    代码地址如下:http://www.demodashi.com/demo/14799.html 前言 Region,即为区域,它表示的是canvas图层上的某一块封闭的区域.很多时候,我们会利用Reg ...

  4. HDU - 4198 Quick out of the Harbour (BFS+优先队列)

    Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...

  5. C#实现多文件上传,写到文件夹中,获取文件信息以及下载文件和删除文件

    前台:.js //上传附件 function uploadAttachment() { if ($("#Tipbind").attr('checked')) { var ip = ...

  6. linux下淘宝安全控件问题

    2009-09-21  我的环境:ubuntu9.04 firefox3.0.14   下载压缩包http://blog.alipay.com/wp-content/2008/10/aliedit.t ...

  7. Go map中一个很重要的特性

    先看一段代码: func main() { m := make(map[int]string) m[1] = "a" m[2] = "b" m[3] = &qu ...

  8. hibernate实现多表联合查询

    转自:http://blog.sina.com.cn/s/blog_67b9ad8d01010by1.html 以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hiber ...

  9. std::string begin end

    std::string 的begin到end是不包含 ‘\0’的

  10. c++ 静态类成员函数(static member function) vs 名字空间 (namespace)

    好多人喜欢把工具函数做成static member function.这样以增加隐蔽性和封装性,由其是从C#,java转而使用c++的开发人员. 例如: class my_math { public: ...