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 题目讲解汇总(持续更新中...)

[LeetCode] Shortest Word Distance II 最短单词距离之二的更多相关文章

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

  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]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

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

  5. LeetCode 243. Shortest Word Distance (最短单词距离)$

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

  6. LeetCode Shortest Word Distance II

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

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

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

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

  9. LeetCode Shortest Word Distance

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

随机推荐

  1. Python爬虫小白入门(一)写在前面

    一.前言 你是不是在为想收集数据而不知道如何收集而着急? 你是不是在为想学习爬虫而找不到一个专门为小白写的教程而烦恼? Bingo! 你没有看错,这就是专门面向小白学习爬虫而写的!我会采用实例的方式, ...

  2. Win10 连接L2TP VPN 失败解决方法

    Win10 连接L2TP VPN 失败解决方法 iOS系统不知道在什么时候,已经不支持PPTP VPN.偶尔的机会刚好看到github上的一键式VPN服务器部署脚本setup-ipsec-vpn,就在 ...

  3. 浅析linux内核中的idr机制

    idr在linux内核中指的就是整数ID管理机制,从本质上来说,这就是一种将整数ID号和特定指针关联在一起的机制.这个机制最早是在2003年2月加入内核的,当时是作为POSIX定时器的一个补丁.现在, ...

  4. spring笔记6 spring IOC的中级知识

    1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...

  5. centos下升级mysql后遇到的小问题

    记录今天遇到的一个小问题, 写一个app访问接口涉及到通过存储过程反馈多个结果集,但是反回多个结果集的存储过程,调用之后只能反回一个了,而且奇怪的是,即使直接在mysql上同时执行两条查询语句,第一条 ...

  6. 特大喜讯,View and Data API 现在支持中文界面了

    大家经常会问到,使用View and Data API怎么做界面的本地化,来显示中文,现在好消息来了,从v1.2.19起,View and Data API开始支持多国语言界面了.你需要制定版本号为v ...

  7. 百度地图隐藏BMKAnnotationView

    BMKAnnotationview.hidden 失效,只能移除Annotation BMKPinAnnotationView设置setHidden为yes时不能隐藏

  8. http转https 和 微信小程序设置了合法请求域名,小程序一直提示不在合法域名列别中

    hotapp 有免费的https proxy ,可以免费代理请求任何http或者https服务,只要设置好合法域名为https://wxapi.hotapp.cn , 就可以请求网址如请求小程序联盟的 ...

  9. Laravel大型项目系列教程(五)之文章和标签管理

    一.前言 本节教程将大概完成文章和标签管理以及标签关联. 二.Let's go 1.文章管理 首先创建管理后台文章列表视图: $ php artisan generate:view admin.art ...

  10. Java 性能分析工具 , 第 1 部分: 操作系统工具

    引言 性能分析的前提是将应用程序内部的运行状况以及应用运行环境的状况以一种可视化的方式更加直接的展现出来,如何来达到这种可视化的展示呢?我们需要配合使用操作系统中集成的程序监控工具和 Java 中内置 ...