LC 244. Shortest Word Distance II 【lock, Medium】
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.
利用一个字典记录所有相同的字母的位置,然后就是两个有序数组比较最近的元素。
我设想把一个数组插入另一个数组,然后比较。C++ lower_bound大法。
class WordDistance {
private:
unordered_map<string,vector<int>> map;
public:
WordDistance(vector<string> words) {
for(int i=; i<words.size(); i++) map[words[i]].push_back(i);
} int shortest(string word1, string word2) {
vector<int> l1 = map[word1];
vector<int> l2 = map[word2];
int idx = ;
int ret = INT_MAX;
for(int i=; i<l2.size(); i++){
idx = lower_bound(l1.begin(), l1.end(),l2[i]) - l1.begin();
if(idx == l1.size()){
ret = min(ret, l2[i] - l1.back());
}else if(idx == ){
ret = min(ret, abs(l2[i] - l1.front()));
}else{
ret = min(ret, abs(l2[i] - l1[idx]));
ret = min(ret, abs(l2[i] - l1[idx-]));
}
if(ret == ) return ;
}
return ret;
}
};
下面是网上的简单做法,思路差不多,找最小的时候遍历,结果runtime24ms...
class WordDistance {
public:
unordered_map<string, vector<int> > map;
WordDistance(vector<string> words) {
for(int i = ; i < words.size(); i++){
map[words[i]].push_back(i);
}
}
int shortest(string word1, string word2) {
vector<int> v1, v2;
v1 = map[word1];
v2 = map[word2];
int diff = INT_MAX;
for(int i = ; i < v1.size(); i++)
for(int j = ; j < v2.size(); j++)
if(abs(v1[i]-v2[j]) < diff)
diff = abs(v1[i]-v2[j]);
return diff;
}
};
LC 244. Shortest Word Distance II 【lock, Medium】的更多相关文章
- LC 245. Shortest Word Distance III 【lock, medium】
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- [LeetCode#244] Shortest Word Distance II
Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...
- [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 ...
- [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 ...
- 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
- [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 ...
随机推荐
- CSS属性(pading margin)
margin: margin:5px auto;意思上下为5,左右平均居中 margin-top: 20px; 上外边距 margin-right: 3 ...
- Spring-AOP-学习笔记(2)-AspectJ
1.启用@AspectJ,需要下载aspectjweaver.jar <!-- 默认启用动态代理 --><aop:aspectj-autoproxy/> <!-- 注解启 ...
- View相关面试问题-ListView缓存面试问题讲解
什么是ListView: ListView就是一个能数据集合以动态滚动的方式显示在用户界面上的View. ListView适配器模式: 有了Adapter可以将数据源与view进行分离~ ListVi ...
- JS特殊写法
记录下工作中碰到的JS特殊写法 (function(index) { $('#' + id).on("change", function() { me.onChange(this, ...
- CF892E Envy[最小生成树]
题意:有一张 $n$ 个点$ m $条边的连通图.有$Q$ 次询问.每次询问给出 $k[i]$ 条边,问这些边能否同时出现在一棵最小生成树上.$n,m,Q,\sum k\le 500000$. 这题利 ...
- 箭头函数中可改变this作用域,回调函数用箭头函数this指向page,自定义事件用箭头函数this指向undefined
1.回调函数中,用箭头函数改变this的作用域 success: (res)=>{ this.setData({ //此时,this指向page页面 ... }) } 2.自定义事件中,如果使用 ...
- WPF程序发布有关事项
- js差异化继承
var parentObj={ name:"123456", get_name:function(){ return this.name; }, says:function(){ ...
- [HEOI2016&TJOI2016] 排序(线段树)
4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2703 Solved: 1386[S ...
- Konrad and Company Evaluation
F. Konrad and Company Evaluation 参考:[codeforces 1230F]Konrad and Company Evaluation-暴力 思路:题意分析见参考博客. ...