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. Hadoop入门学习笔记---part4

    紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...

  2. golang 使用 iota

    iota是golang语言的常量计数器,只能在常量的表达式中使用. iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(io ...

  3. 如何在SSM项目配置springMVC校验框架validator

    1.在springMVC配置文件配置添加如下信息 <!-- 表单验证框架 --> <bean id="validator" class="org.spr ...

  4. shell脚本规划化模板

    shell脚本规划化模板 Linux运维过程中,shell脚本是不可缺少的工具,但是每个运维人员编程的习惯都不一样,很多时候就是实现某个功能,写出来的脚本都是烂七八糟的.脚本必须规范化,应该从以后几个 ...

  5. mysql数据库乱码的问题解决

    排查原因:打断点,查看到底是在执行存数据库操作之前就已经乱码了,还是存数据库操作后乱码的. 1.前者解决方案: 在web.xml里面加上: <filter> <filter-name ...

  6. Python ZIP 文件创建与读取

    Automate the Boring Stuff 学习笔记 02 Python 内置的 zipfile 模块可以对文件(夹)进行ZIP格式的压缩和读取操作.要进行相关操作,首先需要实例化一个 Zip ...

  7. Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39

    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39 V8   重构iocutilV4,use def iocFact...jettyUtil V ...

  8. 理解Java对象的交互:时钟显示程序

    实现: 结构: 对象:时钟  - 对象:小时                 - 对象:分钟 小时和分钟具有相同属性(值,上限),可以用一个类Display来定义这两个对象: 但是两者之间又具有联系( ...

  9. 【前端构建】WebPack实例与前端性能优化

    计划把微信的文章也搬一份上来. 这篇主要介绍一下我在玩Webpack过程中的心得.通过实例介绍WebPack的安装,插件使用及加载策略.感受构建工具给前端优化工作带来的便利. 壹 | Fisrt 曾几 ...

  10. 深入理解DOM节点类型第五篇——元素节点Element

    × 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...