[LeetCode] Shortest Word Distance I & II & III
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
int idx1 = -, idx2 = -, res = words.size();
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
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.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
class WordDistance {
private:
unordered_map<string, vector<int>> wordidx;
public:
WordDistance(vector<string>& words) {
int n = words.size();
for (int i = ; i < n; ++i) wordidx[words[i]].push_back(i);
} int shortest(string word1, string word2) {
vector<int> &idx1 = wordidx[word1];
vector<int> &idx2 = wordidx[word2];
int m = idx1.size(), n = idx2.size();
int res = INT_MAX, i = , j = ;
while (i < m && j < n) {
res = min(res, abs(idx1[i] - idx2[j]));
if (idx1[i] > idx2[j]) ++j;
else ++i;
}
return res;
}
}; // Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “makes”
, word2 = “coding”
, return 1.
Given word1 = "makes"
, word2 = "makes"
, return 3.
Note:
You may assume word1 and word2 are both in the list.
class Solution {
public:
int shortest(vector<string> &words, string word) {
int pre = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word) {
if (pre != -) res = min(res, i - pre);
pre = i;
}
}
return res;
}
int shortestWordDistance(vector<string>& words, string word1, string word2) {
if (word1 == word2) return shortest(words, word1);
int idx1 = -, idx2 = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};
[LeetCode] Shortest Word Distance I & II & III的更多相关文章
- [Locked] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- [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 ...
- [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 ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- [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] 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 ...
随机推荐
- SSM实战——秒杀系统之创建项目、管理依赖、设计数据库
注:本项目使用Myeclipse开发. 一:项目创建 1:使用Myeclipse创建一个web project,命名为MySeckill,并转换为Maven项目. 2:创建项目文件目录如下: 上面四个 ...
- .NET/C#中对自定义对象集合进行自定义排序的方法
一个集合可否排序,要看系统知不知道排序的规则,像内建的系统类型,int ,string,short,decimal这些,系统知道怎么排序,而如果一个集合里面放置的是自定义类型,比如自己定义了一个Car ...
- mysql日期相关的函数
1.获取当前时间: /** 获得当前日期+时间(date + time)函数:now(), 常用 **/ select now() from dual; /** 获取当前时间戳,current_tim ...
- loadrunner 脚本开发-参数化之将内容保存为参数、参数数组及参数值获取
转自:http://blog.sina.com.cn/s/blog_13cc013b50102v49c.html(查看原文) 在VuGen中默认使用{}的字符串称为参数 注意:参数必须在双引号中才能用 ...
- glog的使用
主要还是看官方文档吧 win32下,把#define GLOG_NO_ABBREVIATED_SEVERITIES 放到#include <windows.h>之前,具体说明文档中有说. ...
- 【laravel5.* + 钉钉实现WEB第三方登录】 使用redis 作为持久化存储
1.去钉钉开发者平台>自助者工具,创建扫码登录授权应用,填写名称.描述.授权页面logo地址(这个图片最后会出现在用户扫码设备中,建议使用压缩图片减少用户加载时间).回调域名(一般都是写一个子域 ...
- QQ登录整合/oauth2.0认证-03-对第二节的代码改进
---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...
- usr/bin/X11各个程序中文详解
X11程序 animate 输出图形结果 bitmap bmtoa bounce 输出X屏幕保存结果 display 浏览编辑image magick图像 editres 编辑X11资源 flsfon ...
- 听听各位对Ubuntu的UI的看法
2012-7-15 15:46 最近升级到Ubuntu 12.4 .发现其界面效果真的时越来越炫啦.我就在想,你Ubuntu你图什么啊, 你是以个Linux系统,你的重点在于让系统运行更稳定,更 ...
- c++ remove_if
#include <algorithm> 函数remove_if()移除序列[start, end)中所有应用于谓词p返回true的元素. 此函数返回一个指向被修剪的序列的最后一个元素迭代 ...