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. Your method will be called repeatedly many times with different parameters.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

这道题是之前那道 Shortest Word Distance 的拓展,不同的是这次我们需要多次调用求最短单词距离的函数,那么用之前那道题的解法二和三就非常不高效,而当时摒弃的解法一的思路却可以用到这里,这里用 HashMap 来建立每个单词和其所有出现的位置的映射,然后在找最短单词距离时,只需要取出该单词在 HashMap 中映射的位置数组进行两两比较即可,参见代码如下:

解法一:

class WordDistance {
public:
WordDistance(vector<string>& words) {
for (int i = ; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
} int shortest(string word1, string word2) {
int res = INT_MAX;
for (int i = ; i < m[word1].size(); ++i) {
for (int j = ; j < m[word2].size(); ++j) {
res = min(res, abs(m[word1][i] - m[word2][j]));
}
}
return res;
} private:
unordered_map<string, vector<int> > m;
};

我们可以优化上述的代码,使查询的复杂度由上面的 O(MN) 变为 O(M+N),其中M和N为两个单词的长度,需要两个指针i和j来指向位置数组中的某个位置,开始初始化都为0,然后比较位置数组中的数字,将较小的一个的指针向后移动一位,直至其中一个数组遍历完成即可,参见代码如下:

解法二:

class WordDistance {
public:
WordDistance(vector<string>& words) {
for (int i = ; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
} int shortest(string word1, string word2) {
int i = , j = , res = INT_MAX;
while (i < m[word1].size() && j < m[word2].size()) {
res = min(res, abs(m[word1][i] - m[word2][j]));
m[word1][i] < m[word2][j] ? ++i : ++j;
}
return res;
} private:
unordered_map<string, vector<int> > m;
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/244

类似题目:

Shortest Word Distance

Shortest Word Distance III

Merge Two Sorted Lists

参考资料:

https://leetcode.com/problems/shortest-word-distance-ii/

https://leetcode.com/problems/shortest-word-distance-ii/discuss/67028/Java-Solution-using-HashMap

https://leetcode.com/problems/shortest-word-distance-ii/discuss/67066/9-line-O(n)-C%2B%2B-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. 【初探HTML本相】道之真谛不过自然,html标签脱俗还真
  2. mysql添加用户和用户权限
  3. Intent的属性介绍
  4. 期望dp-hdu-4336-Card Collector
  5. 于Eclipse传导C/C++配置方法开发(20140721新)
  6. XtraGrid滚轮翻页
  7. [POJ1631] nlogn求LIS
  8. 安装php扩展 ffmpeg-php
  9. 【unix网络编程第三版】ubuntu端口占用问题
  10. Java集合的总结
  11. hibernate学习(初识)
  12. Tecplot: Legend显示与否
  13. 30天代码day2 Operators
  14. Android Studio无法识别手机
  15. win10传奇手册CHM打开无法阅读解决
  16. docker --Dockerfile--最小java环境
  17. 转载:2.2 Nginx配置的通用语法《深入理解Nginx》(陶辉)
  18. apiCloud 选择图片,选择视频,压缩图片
  19. nodejs sequelize 对应数据库操作符的定义
  20. [原] unity3d动态加载脚本

热门文章

  1. PHP实现全排列(递归算法)
  2. QT 删除文件指定目录
  3. C#开发微信门户及应用(21)-微信企业号的消息和事件的接收处理及解密
  4. Hive学习笔记(一)
  5. Mysql FROM_UNIXTIME效率 VS PHP date()效率 数据说话!
  6. hadoop 2.7.2 + zookeeper 高可用集群部署
  7. Jmeter3.0发布,版本更新都更新了什么
  8. HttpClient调用webApi时注意的小问题
  9. MySql 修改列的注释信息的方法
  10. jQuery中的$.extend方法来扩展JSON对象及合并,方便调用对象方法