2014-04-29 01:51

题目:你有一个文本文件,每行一个单词。给定两个单词,请找出这两个单词在文件中出现的其中一对位置,使得这两个位置的距离最短。

解法:我的思路是建立倒排索引,计算出所有单词出现的所有位置。下面代码只给出了两个索引的处理方法。倒排索引一般以链表的形式出现,通过顺序扫描两个链表,类似归并排序算法中的merge过程,就能够找出最短距离。请看代码。

代码:

 // 18.5 Given a text file containing words, find the shortest distance between two words.
#include <algorithm>
#include <climits>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int _val = ): val(_val), next(nullptr) {};
}; class Solution {
public:
int minDist(ListNode *head1, ListNode *head2)
{
// the two list records the forward indices of two word, i.e. their occurrence position.
// For example, 1->4->5->nullptr means the word appears at 1st, 4th and 5th line. int min_dist = INT_MAX;
ListNode *p1, *p2; p1 = head1;
p2 = head2;
while (p1 != nullptr && p2 != nullptr) {
if (p1->val < p2->val) {
min_dist = min(min_dist, p2->val - p1->val);
p1 = p1->next;
} else {
min_dist = min(min_dist, p1->val - p2->val);
p2 = p2->next;
}
} return min_dist;
}
};

《Cracking the Coding Interview》——第18章:难题——题目5的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. 笨办法学Python(三十五)

    习题 35: 分支和函数 你已经学会了 if 语句.函数.还有列表.现在你要练习扭转一下思维了.把下面的代码写下来,看你是否能弄懂它实现的是什么功能. from sys import exit def ...

  2. meta详解(常用)

    1.<meta http-equiv="X-UA-Compatible" content="IE=edge"> 说明:设置浏览器的兼容模式版本.表示 ...

  3. sublim插件(待续)

    imesupport SublimeText3默认不支持输入法跟随光标,这在输入中文的时候看起来不方便. 进入SublimeText3在上面菜单栏里Perferences点击PackageContro ...

  4. 用命令关键字(Cmdlet Keyworlds)编写面向管道的脚本

    使用begin  process和end关键字 把你的脚本分成 初始化 处理和清楚几个区域

  5. CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]

    启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...

  6. Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70198541 本文出自方志朋的博客 今天一时兴起,想用python爬爬自己的博客,通过数据 ...

  7. 深入浅出:promise的各种用法

    https://mp.weixin.qq.com/s?__biz=MzAwNTAzMjcxNg==&mid=2651425195&idx=1&sn=eed6bea35323c7 ...

  8. mybatis中oracle转mysql

    刚来公司实习,遇到的第一个任务就是这个,简单记录一下思路过程.人菜的很,没啥参考价值. 测试时: 将现有的oracle库转为mysql: 用的Navicat自带数据传输功能,简单粗暴 出现的问题: 1 ...

  9. cordova创建工程添加插件

    创建工程 phonegap创建工程 代码 用以创建自己需要的  工程名   ; 报名  ;类名 ; 应用名 cordova create hello com.example.hello HelloWo ...

  10. git push 时 fatal: Unable to create 'D:/phpStudy/WWW/green_tree/.git/index.lock': File exists.解决办法

    找到自己的项目,找到.git文件夹,进去把目标文件删除即可 或者使用rm -rf 命令(如果没有那个文件件或者文件,将隐藏文件打开就可以看到了)