2014-03-21 21:37

题目:给定一个字符串数组,但是其中夹杂了很多空串“”,不如{“Hello”, “”, “World”, “”, “”, “”, “Zoo”, “”}请设计一个算法在其中查找字符串。

解法:要么一次性将其中夹杂的空串去掉,要么在二分查找的过程中逐个跳过空串。反正整体思路仍是二分。

代码:

 // 11.5 Given an array of strings interspersed with empty string ""s. Find out if a target string exists in the string.
#include <iostream>
#include <string>
#include <vector>
using namespace std; int main()
{
int i, j, n;
int ns;
string s;
vector<string> v;
int ll, rr, mm; while (cin >> n && n > ) {
cin >> ns;
for (i = ; i < ns; ++i) {
v.push_back("");
}
for (i = ; i < n; ++i) {
cin >> s;
v.push_back(s);
cin >> ns;
for (j = ; j < ns; ++j) {
v.push_back("");
}
}
cin >> s; ll = ;
while (v[ll] == "") {
++ll;
}
rr = (int)v.size() - ;
while (v[rr] == "") {
--rr;
}
while (ll <= rr) {
mm = (ll + rr) / ;
while (v[mm] == "") {
// go left or right, either is OK.
--mm;
}
if (s > v[mm]) {
ll = mm + ;
while (ll <= rr && v[ll] == "") {
++ll;
}
} else if (s < v[mm]) {
rr = mm - ;
while (rr >= ll && v[rr] == "") {
--rr;
}
} else {
break;
}
}
printf("%d\n", (ll <= rr ? mm : -)); v.clear();
} return ;
}

《Cracking the Coding Interview》——第11章:排序和搜索——题目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》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

  10. 《Cracking the Coding Interview》——第11章:排序和搜索——题目2

    2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...

随机推荐

  1. ring0 SSDTHook 实现x64/x86

    #include "HookSSDT.h" #include <ntimage.h> #define SEC_IMAGE 0x001000000 ULONG32 __N ...

  2. 修改FileZilla(FTP客户端)同时传输的文件数

    在菜单中点击“编辑”.“设置”,按照以下步骤操作:

  3. 智能开关:orange pi one(arm linux)控制继电器

    大家都知道,继电器是用小电流去控制大电流运作的一种“自动开关”,在我们生活.工作中随处可见.现在的“智能家居”概念,有很多功能模块其实就是“智能开关”,远程开关.定时开关.条件触发开关等等. 下面介绍 ...

  4. javascript中parseInt(),08,09,返回0

    javascript中在使用parseInt(08).parseInt(09),进行整数转换的时候,返回值是0 工具/原料   浏览器 文本编辑器 方法/步骤     javascript中在使用pa ...

  5. 使用taobao cnpm 源解决npm无法安装module问题

    npm 安装nativescript时出现异常,一直停着不动.应该是源被墙了的问题可以使用淘宝仓库,执行下面的命令: alias cnpm="npm --registry=https://r ...

  6. 1012: A MST Problem

    1012: A MST Problem 时间限制: 1 Sec  内存限制: 32 MB提交: 63  解决: 33[提交][状态][讨论版][命题人:外部导入] 题目描述 It is just a ...

  7. 用js给元素加css

    1.如果是没有CSS文件,或者要修改的不在CSS文件里,那么: document.getElementById('DIV标签的ID').style.属性='属性值'; 这样就可以了.2.如果,样式是写 ...

  8. tp5 验证是不是ajax提交

    话不多说,看代码 if(request()->isAjax()){ return "是ajax提交"; }else{ return "不是ajax提交"; ...

  9. vscode-tfs插件报错:TF30063

    解决方案:删除tfs凭证,然后用vs重新登陆tfs服务器,此时会在电脑上创建要一个新的tfs凭证,然后再用vscode-tfs操作tfs就没有问题了.

  10. 第50章 读写内部FLASH—零死角玩转STM32-F429系列

    第50章     读写内部FLASH 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...