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】的更多相关文章

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

  2. [LC] 244. Shortest Word Distance II

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

  3. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

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

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

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

  7. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  8. 244. Shortest Word Distance II 实现数组中的最短距离单词

    [抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...

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

随机推荐

  1. sql分页查询(2005以后的数据库)和access分页查询

    sql分页查询: select * from ( select ROW_NUMBER() over(order by 排序条件) as rowNumber,* from [表名] where 条件 ) ...

  2. Oracle【增删改&数据的备份】

    增删改的SQL语句执行完毕后,不会立马进行数据的写入数据库(这时数据在内存中),需要手动对数据进行提交(commit),如果数据出问题,可以使用回滚.主键:非空唯一的 --在一张表中,某字段值是非空唯 ...

  3. python函数:叠加装饰器、迭代器、自定义迭代器、生成式

    一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...

  4. Linux配置JDK环境

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  5. li元素之间产生间隔

    是因为li标签换行导致的 简单的解决办法是将所有的li标签写到一行(不过实际上一般不会这样做) 或者把ul设置font-size为0,但这样ul中的文字就会消失,所以要记得单独给子元素设置font-s ...

  6. 自动生成SSM框架

    使用idea 新创建项目 然后 新创建 java .resources 文件夹...... 图上是项目结构 java文件夹下的 文件夹 命名规范 com.nf147(组织名)+ oukele(作者) ...

  7. java 和 c#返回值方法

    java 和 c#都是应用很广泛的语言,也互有优劣. 这两者都是面向对象的语言,在一个方法中如果类型不是void那么是需要return一个返回值的. 但是如果想要返回多个值该怎么办? 排除直接返回一个 ...

  8. 【Wince-自定义控件】ImageButton 带图片、文字

    1.看图 可以实现MouseDown改变背景颜色或背景图片. 遗憾是没有实现键盘触发按钮事件. 2.选择继承自Control基类 public class ImageButton : Control ...

  9. jquery keyup()方法 语法

    jquery keyup()方法 语法 作用:完整的 key press 过程分为两个部分,按键被按下,然后按键被松开并复位.当按钮被松开时,发生 keyup 事件.它发生在当前获得焦点的元素上.ke ...

  10. Subarray Sorting (线段树)

    题意:给你两个长度为 n 的序列 a 和 b , 可以对 a 进行 操作: 选择一段区间[ l, r ] ,使得序列a 在这段区间里 按升序排序. 可以对a 进行任意多次操作,问 a是否有可能变成b序 ...