11.5 Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string.
 EXAMPLE
 Input: find "ball" in {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""}
 Output: 4

这道题给了我们一个有序的字符串数组,但是在中间加入了很多空字符串,让我们来查找一个给定字符串。如果没有这些空字符串,那么我们用二分查找法很容易搜索,但是这些空字符串就有很大的干扰作用。那么我们要在原有的二分查找法上做修改,类似的修改二分查找法的里有Search in Rotated Sorted Array 在旋转有序数组中搜索Search in Rotated Sorted Array II 在旋转有序数组中搜索之二。这道题我们的思路是,查找中间的字符串,如果是空字符串,那么我们用二分查找法来找周围最近的非空字符串,然后把mid移到飞空字符串的位置,继续二分查找。相当于二分查找中又嵌套了一个二分查找,参见代码如下:

class Solution {
public:
int search(vector<string> strings, string str) {
int first = , last = strings.size() - ;
while (first <= last) {
int mid = first + (last - first) / ;
if (strings[mid].empty()) {
int left = mid - , right = mid + ;
while (true) {
if (left < first && right > last) return -;
else if (right <= last && !strings[right].empty()) {
mid = right; break;
}
else if (left >= first && !strings[left].empty()) {
mid = left; break;
}
++right;
--left;
}
}
int res = strings[mid].compare(str);
if (res == ) return mid;
else if (res < ) first = mid + ;
else last = mid - ;
}
return -;
}
};

[CareerCup] 11.5 Search Array with Empty Strings 搜索含有空字符串的数组的更多相关文章

  1. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  2. [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

    11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...

  3. [CareerCup] 18.8 Search String 搜索字符串

    18.8 Given a string s and an array of smaller strings T, design a method to search s for each small ...

  4. LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组   中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...

  5. atitit.vod search doc.doc 点播系统搜索功能设计文档

    atitit.vod search doc.doc 点播系统搜索功能设计文档 按键的enter事件1 Left rig事件1 Up down事件2 key_events.key_search = fu ...

  6. 固定分隔符字符串与数组互转及ArrayList与数组(Array)互转

    1.字符串转数组 这个相信多数人都会常用,string.split方法,分隔符可以为多个.详细信息参见MSDN string[] actionCfgs = _para.Split(new char[] ...

  7. Linux 中C/C++ search path(头文件搜索路径)

    https://blog.csdn.net/BjarneCpp/article/details/76135980 起因 我拿到了一套Linux下的C++代码,代码中有这个头文件#include < ...

  8. Elasticsearch 搜索模块之Cross Cluster Search(跨集群搜索)

    Cross Cluster Search简介 cross-cluster search功能允许任何节点作为跨多个群集的federated client(联合客户端),与tribe node不同的是cr ...

  9. php array 根据value获取key,in_array()判断是否在数组内实例

    php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...

随机推荐

  1. Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

    Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Th ...

  2. LevelDB源码分析--使用Iterator简化代码设计

    我们先来参考来至使用Iterator简化代码2-TwoLevelIterator的例子,略微修改希望能帮助更加容易立即,如果有不理解请各位看客阅读原文. 下面我们再来看一个例子,我们为一个书店写程序, ...

  3. 学习随笔—Redis常用命令

    info 服务器基本信息 monitor 实时转储收到的请求 flushdb 清空当前数据库 flushall 清空所有数据库 quit 关闭连接 save 将数据同步保持到磁盘 bgsave     ...

  4. nginx服务器中的安全配置

    一.关闭SELinux 安全增强型Linux(SELinux)的是一个Linux内核的功能,它提供支持访问控制的安全政策保护机制. 但是,SELinux带来的附加安全性和使用复杂性上不成比例,性价比不 ...

  5. Progress Control with Text

    原文链接:http://www.codeproject.com/Articles/80/Progress-Control-with-Text 重写的Progress 包括,设置bar前景背景颜色,设置 ...

  6. JS的三种弹框

    <script type="text/javascript"> /*第一种*/ function ale(){ alert("这是第一种");} / ...

  7. 常用awk命令(转)

    awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输 ...

  8. python 标准库和第3方库的介绍

    忘了从哪里来的了~~~~ Tkinter———— Python默认的图形界面接口.Tkinter是一个和Tk接口的模块,Tkinter库提供了对Tk API的接口,它属于Tcl/Tk的GUI工具组.T ...

  9. Mybaits学习总结1

    http://www.cnblogs.com/xdp-gacl/p/4261895.html 参考了这篇文章搭建了Mybaits环境,原作者有些地方没有标注使用某种编码,我是自学SQL的,所以深知编码 ...

  10. 日期选择器:jquery datepicker的使用

    helloweba.com 作者:月光光 时间:2012-04-08 21:05 标签: jquery  datepicker  jquery ui     在jquery ui中,提供了一个非常实用 ...