《Cracking the Coding Interview》——第11章:排序和搜索——题目5
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目2
2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...
随机推荐
- Python用户交互与流程控制
1. 用户交互 python3通过input实现用户交互,与python2的raw_input一样,接收的值都转换成字符串格式.python2中也有一个input,而python2中的input接收的 ...
- 笨办法学Python(十二)
习题 12:提示别人 当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”.这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s ...
- 关于安卓手机访问一些网站或者Fiori应用弹出安装证书的提示
有朋友问遇到在安卓手机上安装Fiori Client,打开的时候提示需要安装证书,如下图所示: 我在自己的Android手机试了试,因为我没有装Fiori Client,所以就用手机浏览器直接访问ht ...
- UI5 Source code map机制的细节介绍
在我的博客A debugging issue caused by source code mapping里我介绍了在我做SAP C4C开发时遇到的一个曾经困扰我很久的问题,最后结论是这个问题由于Jav ...
- Xcode SDK模拟器安装及安装路径
将SDK想要装的版本,将SDK包放入‘mac中的SDK安装路径’.再将Xcode模拟器重启. 再打开Xcode模拟器,就可以在菜单栏的 ‘硬件’->’设备‘->’iPhone Retina ...
- node 上的cookie的签名和解签名
cookie签名的原因是防止别人篡改cookie原本的值,如果这个过程中cookie被改变的话,就会在unsign方法返回false 代码: var cookie = require("co ...
- vuejs中v-if的深层用法v-else,v-else-if,key
<div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...
- 使用Excel管理命令输出
效果图:(饼状图为后添加) 实现代码:
- ubuntu 报错E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unav E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process us
1.配置xshell,查看虚拟机中ubuntu中网络ip ifconfig 报错 Command 'ifconfig' not found, but can be installed with: su ...
- office2010
MS office2010 360网盘:http://yunpan.cn/QajXaRWbnpTzF (提取码:cf72) 如何激活参见我下面的博客: http://www.cnblogs.com/l ...