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. uCos-II移值(二)

    os_cpu_c.c文件 该文件主要是根据处理器平台特点完成任务堆栈初始化函数OSTaskStkInit以及其他几个用户Hook函数的编写,其中必须要实现的函数是OSTaskStkInit(在创建任务 ...

  2. 解决docker容器的窗口大小问题

    解决docker容器的窗口大小问题 最近哥们在是使用docker时,发现有些容器内部窗口大小有问题. 如下午所示,vi窗口只占据左上角一部分.正常情况下vi应该铺满整个窗口才对呀. 所以哥们找到了解决 ...

  3. VMware提示此主机支持Intel VT-x,但Intel VT-x处于禁用状态怎么解决

    本文链接:https://blog.csdn.net/weixin_40816738/article/details/90146770 ThinkPad笔记本1.开机按F1或Fn+F1进入BIOS,切 ...

  4. IDA 头像是谁

    IDA图标上的女子:Ada Lovelace Ada Lovelace 简介: 阿达·奥古斯塔,19世纪诗人拜伦的女儿,数学家.穿孔机程序创始人,建立了循环和子程序概念.为计算程序拟定“算法”,写作的 ...

  5. 解决Windows jmeter Non HTTP response message: Address already in use: connect 错误(转载)

    jMeter报错: Response code: Non HTTP response code: java.net.BindExceptionResponse message: Non HTTP re ...

  6. Qt 多线程使用moveToThread

    Qt有两种多线程的方法,其中一种是继承QThread的run函数,另外一种是把一个继承于QObject的类用moveToThread函数转移到一个Thread里. Qt4.8之前都是使用继承QThre ...

  7. 【Android-NetWork】 判断是否连接网络,判断网络连接方式

    如何判断Android是否连接网络? Java代码: ConnectivityManager conn = (ConnectivityManager) getSystemService(Activit ...

  8. 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer

    题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...

  9. jquery fadeOut()方法 语法

    jquery fadeOut()方法 语法 作用:fadeOut() 方法使用淡出效果来隐藏被选元素,假如该元素是隐藏的.大理石平台精度等级 语法:$(selector).fadeOut(speed, ...

  10. 《剑指offer》算法题第六天

    今日题目: 顺时针打印矩阵 包含min函数的栈 栈的压入.弹出序列 从上到下打印二叉树 二叉树搜索树的后序遍历序列 二叉树中和为某一值的路径 今天的题目都比较简单,下面一一介绍: 1. 顺时针打印矩阵 ...