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. centos 7中磁盘挂载重启后挂载失效

     在centos 7磁盘挂载成功后,关机重启,挂载磁盘失效,需要重新挂载,不用重新挂载的开机挂载方法如下: 1.先检验要挂载的磁盘是否已被挂载,有的话先卸除 2.修改 /etc/fstab 文件 ,最 ...

  2. 笨办法学Python(十五)

    习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...

  3. 屏蔽各类弹窗广告(WPS、智能云输入法)

    托盘中的广告“领取双11红包,最高1111元”的罪魁祸首是“智能云输入法” 广告在托盘中闪动: 结束SCSkinInst.exe后,托盘中的广告消失: 智能云输入法的安装路径可参考: C:\Progr ...

  4. thinkphp 去掉URL 里面的index.php

    例如你的原路径是 http://localhost/test/index.php/home/goods/index.html 那么现在的地址是 http://localhost/test/home/g ...

  5. IOS tableView的基本使用

    tableView  Style:Plain(头部标题 向上移 不会消失) tableView  Style:Grouped(头部标题 向上移 会 消失) #import "ViewCont ...

  6. tensorflow与android编译

    我的过程: 1.下载tensorflow 2.下载ndk.sdk然后放到了tensorflow的目录下 3,修改workspace 4.运行命令:bazel build -c opt //tensor ...

  7. C语言学生成绩管理系统(简易版)

    #include<stdio.h> #include<stdlib.h> #include<string.h> int readstudents(struct st ...

  8. iso十款常用类库

    iso十款常用类库 MBProgressHUD(进展指示符库)   地址:https://github.com/jdg/MBProgressHUD   苹果的应用程序一般都会用一种优雅的,半透明的进度 ...

  9. JS实现数组去重的方法(6种)

    方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Arra ...

  10. 51nod 1298 圆与三角形——计算几何

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1298 转化成判断三条线段和圆是否