《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 ...
随机推荐
- Simotion应用案例,使用Simotion web server调试,使用Project Generator创建项目,Simosim模拟运行运行项目
Simotion web server simotion项目设计和调试过程中,web server功能越来越常用.例如Project generator生成的FBAxis, winder, print ...
- uva题库爬取
每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...
- Nodejs事件监听模块
nodejs里面是不存在浏览器里面都冒泡,捕获这些行为的,所以Nodejs实现了events这个模块,里面大多数的模块都集成了这个模块,所以events是node模块里面最重要都一个模块,他对外只暴露 ...
- 将xml转换成Json,数组,对象格式转换方法
xml字符串:$simplexml 转换成Json格式:json_encode($simplexml) 转换成数组格式:json_decode(json_encode($simplexml),TRUE ...
- Task 的入门
https://www.cnblogs.com/huangxincheng/archive/2012/04/03/2430638.html
- CDH4.5.0下安装lzo
参考 http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/1.0.1/Installing-and-Using- ...
- python解析ini文件
python解析ini文件 使用configparser - Configuration file parser sections() add_section(section) has_section ...
- Vue 前端md5加密
用户注册时将加密后的密码发送给后端存储 当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配. npm: https://www.npmjs.com/package/crypto-browseri ...
- 使用Git操作码云
一.安装并配置 .安装git 下载地址: 官方网站:https://git-for-windows.github.io/ 国内镜像:https://pan.baidu.com/s/1kU5OCOB#l ...
- Inventory Update-freecodecamp算法题目
Inventory Update 1.要求 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新 ...