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 ...
随机推荐
- centos 7 firewall(防火墙)开放端口/删除端口/查看端口
1.firewall的基本启动/停止/重启命令 复制#centos7启动防火墙 systemctl start firewalld.service #centos7停止防火墙/关闭防火墙 system ...
- 第五章· MySQL数据类型
一.数据类型介绍 1.四种主要类别  1)数值类型 2)字符类型 3)时间类型 4)二进制类型 2.数据类型的 ABC 要素 1)Appropriate(适当) 2)Brief(简洁) 3)Comp ...
- 工控漏洞利用框架 - ISF(Industrial Security Framework)
一. 框架介绍 本框架主要使用Python语言开发,通过集成ShadowBroker释放的NSA工具Fuzzbunch攻击框架,开发一款适合工控漏洞利用的框架.由于Fuzzbunch攻击框架仅适用于P ...
- 构建虚拟工控环境系列 - 西门子虚拟PLC
一. 概述 跟随着工控安全一路走来,工控安全市场今年明显有相当大的改善,无论从政策还是客户需求,都在逐步扩大中.但是,搞工控安全研究的人员却寥寥无几.一方面工控安全是个跨学课的技术,需要了解多方面的知 ...
- vim复制到剪切板
作者:whinc链接:https://www.zhihu.com/question/19863631/answer/89354508来源:知乎 转载文章 Vim 中的复制.删除的内容都会被存放到默认( ...
- tbdr+mrt
有关mrt的在tbdr的架构下的内存排布 system memory肯定是dither 我对这里把握比较大 rt0 rgba8 rt1 r8 这样像素排列是rgba8r8rgba8r8rgba8r8. ...
- Storm实践(二):集群搭建
集群规划 角色 IP hostname nimbus 192.168.100.101 dda supervisor 192.168.100.102 ddb supervisor 192.168.100 ...
- PHP Swoole websocket协议实现
- nginx准备
iptables相关命令 iptables -L查看相关规则 iptables -F关闭所有的防火墙规则 getenforce 查看selinux是否开启 ,如果已经开启,可以通过setenforc ...
- 小猿圈-IT自学人的小圈子 https://book.apeland.cn/details/54/
笔记链接 https://book.apeland.cn/details/54/ 学习视频 https://www.apeland.cn/python