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 DistanceShortest 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 All in One 题目汇总

[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离的更多相关文章

  1. [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 ...

  2. maximum shortest distance

    maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  3. PAT1046: Shortest Distance

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  4. [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 ...

  5. 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 ...

  6. [LeetCode] Shortest Distance to a Character 到字符的最短距离

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  7. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  8. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

  9. A1046. Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

随机推荐

  1. ListView中每个item条目在被单击选中时能够高亮显示

    在布局文件中设定: android:listSelector="@android:color/holo_red_light" 在代码中实现 listView.setSelector ...

  2. php echo return exit 区别

    echo.print().printf().sprintf().vardump().varexport():都可以输出内容到网页,但不退出函数或程序. return:返回并立即退出,函数级别. die ...

  3. 汇编学习(四)——算术运算程序

    (一)跳转指令 一.无条件跳转指令(不管标志寄存器,执行到这句直接跳转) 1.段内直接跳转指令 (1)指令格式: JMP SHORT short_label; IP<--IP+DB,即代码直接跳 ...

  4. 【maven】maven各种奇葩问题

    问题1:Could not calculate build plan: Plugin org.apache... 不能成功创建maven项目 解决方法1: http://repo1.maven.org ...

  5. 把textarea右下角的灰点去掉

    这个是浏览器自带功能,那个区域属于滚动条的占位区 使textarea可以调整size,加 style="resize:none"可解决,添加多行文字时还是会出现滚动条的.

  6. MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

    主服务器上(注:应该是允许从机访问)  GRANT REPLICATION SLAVE ON *.* to ‘rep1’@’192.168.10.131’ identified by ‘passwor ...

  7. SpringMVC解析3-DispatcherServlet组件初始化

    在spring中,ContextLoaderListener只是辅助功能,用于创建WebApplicationContext类型实例,而真正的逻辑实现其实是在DispatcherServlet中进行的 ...

  8. const int *

    5.Please choose the right statement about constusage: A.const int a;//const interger B.int const a;/ ...

  9. 简单的jQuery扩展函数-让函数缓冲执行

    $.fn.retarder = function(delay, method) { var node = this; if (node.length) { if (node[0]._timer_) c ...

  10. 【转载】Android端手机测试体系

    1.冒烟测试 跟web端 的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做 我们的测试工具.就跟我之前博 ...