[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离
18.5 You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?
LeetCode上的原题,请参见我之前的博客Shortest Word Distance,Shortest Word Distance II和 Shortest Word Distance III。
解法一:
// Call One Time
int shortest_dist(vector<string> words, string word1, string word2) {
int p1 = -, p2 = -, res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = i;
if (words[i] == word2) p2 = i;
if (p1 != - && p2 != -) res = min(res, abs(p1 - p2));
}
return res;
}
解法二:
// Call Many Times
int shortest_dist(vector<string> words, string word1, string word2) {
unordered_map<string, vector<int>> m;
int i = , j = , res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
while (i < m[word1].size() && j < m[word2].size()) {
res = min(res, abs(m[word1][i] - m[word2][j]));
m[word1][i] < m[word2][j] ? ++i : ++j;
}
return res;
}
解法三:
// word1, word2 may be same
int shortest_dist(vector<string> words, string word1, string word2) {
int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
if (words[i] == word2) p2 = i;
res = min(res, abs(p1 - p2));
}
return res;
}
[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离的更多相关文章
- [Locked] Shortest Distance from All Buildings
Shortest Distance from All Buildings You want to build a house on an empty land which reaches all bu ...
- maximum shortest distance
maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT1046: Shortest Distance
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- LeetCode 613. Shortest Distance in a Line
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- A1046. Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
随机推荐
- AIX下禁止crs随ha启动而启动
/etc/init.crs enable /etc/init.crs disable 查看目前crs是enable还是disable状态 状态记录在一个文本文件里 /etc/oracle/scls_ ...
- 第九篇:在SOUI中使用多语言翻译
为UI在不同地区显示不同的语言是产品国际化的一个重要要求. 在SOUI中实现了一套类似QT的多语言翻译机制:布局XML不需要调整,程序代码也不需要调整,只需要为不同地区的用户提供不同的语言翻译文件即可 ...
- android倒计时(整理)
android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本 ...
- 汇编学习(二)——8086CPU
一.8086CPU 1.微处理器 (1)微控制机:也称单片机 (2)DSP芯片:数字信号处理芯片 (3)嵌入式微处理器 (4)通用微处理器:PC站.工作站.服务器使用的处理器 2.内部结构: (1)总 ...
- Handler(消息机制)
Demo演示 //通过Handler事件倒计时的一个操作,并判断状态 public class MainActivity extends AppCompatActivity {private Text ...
- open文件操作
open()做文件操作的就是他1.打开文件#f=open("db","r")#只读#f-open("db","w")#只 ...
- 手持终端PDA应用固定资产管理系统(资产查询 盘点)软件程序系统
一.产品概述 固定资产管理系统,是针对企事业单位内部资产管理中出现的工作量大.过程繁琐.追踪困难等一系列难题开发的一套先进管理软件.软件实现了对资产的多种方式管理,目前包括条形码.二维码.RFID管理 ...
- delphi 中TStringList Clear 方法的时候该对象有没有被释放
delphi 中TStringList 通过function AddObject(const S: string; AObject: TObject): Integer; 方法添加了一个对象,请问我在 ...
- Static Resources In ASP.NET Core 1.0
静态资源包括HTML,CSS,图片和Js文件.在ASP.NET Core 1.0中,静态资源默认的所在目录是wwwroot,wwwroot可以在project.json中定义. Steps: 在www ...
- address元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...