[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 ...
随机推荐
- 上四条只是我目前总结菜鸟们在学习FPGA时所最容易跑偏的地
长期以来很多新入群的菜鸟们总 是在重复的问一些非常简单但是又让新手困惑不解的问题.作为管理员经常要给这些菜鸟们普及基础知识,但是非常不幸的是很多菜鸟怀着一种浮躁的心态来学习 FPGA,总是急于求成. ...
- C++重载覆盖隐藏
写一个程序,各写出重载覆盖 1 // // main.cpp // 2013-7-17作业2 // // Created by 丁小未 on 13-7-17. // Copyright (c) 201 ...
- Linux学习笔记(11)软件包管理
Linux中的软件包分为源码包(脚本安装包)及二进制包(RPM包.系统默认包).其中源码包的优点是: 1)源码包是开源的,如果有足够的能力,可以修改源代码: 2)可自由选择所需的功能: 3)源码包需编 ...
- Laravel 之Service Providers
Service providers are the central place of all Laravel application bootstrapping. Your own applicati ...
- caffe安装(linux)
从官网github下载caffe-master.zip 解压:unzip caffe-master 将Makefile.config.example复制,命名为Makefile.config(如果是C ...
- Oracle常用命令
查看数据库锁的情况 SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$ ...
- opacity与rgba
background: rgba(255,255,255,0.6);容器本身透明度变化,它包含的子容器的透明度不变. opacity:0.6;容器及容器包含的子容器的透明度都会发生变化.
- Matlab 查看变量信息who whos命令
- iOSDay27之界面通信
1. 属性传值(前面的界面给后面传值) 第一步: 在 SecondViewController.h 文件里定义一个存放传过来值的变量 contents #import <UIKit/UIKit. ...
- BZOJ 1901 & 整体二分
题意: 带修改的区间第K小. SOL: 看了很久很久很久很久的整体二分,网上的各种题解也不是很多,也一直很不了解所谓的"贡献","将询问一起递归"是什么意思.. ...