[CareerCup] 11.5 Search Array with Empty Strings 搜索含有空字符串的数组
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 搜索含有空字符串的数组的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组 中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...
- atitit.vod search doc.doc 点播系统搜索功能设计文档
atitit.vod search doc.doc 点播系统搜索功能设计文档 按键的enter事件1 Left rig事件1 Up down事件2 key_events.key_search = fu ...
- 固定分隔符字符串与数组互转及ArrayList与数组(Array)互转
1.字符串转数组 这个相信多数人都会常用,string.split方法,分隔符可以为多个.详细信息参见MSDN string[] actionCfgs = _para.Split(new char[] ...
- Linux 中C/C++ search path(头文件搜索路径)
https://blog.csdn.net/BjarneCpp/article/details/76135980 起因 我拿到了一套Linux下的C++代码,代码中有这个头文件#include < ...
- Elasticsearch 搜索模块之Cross Cluster Search(跨集群搜索)
Cross Cluster Search简介 cross-cluster search功能允许任何节点作为跨多个群集的federated client(联合客户端),与tribe node不同的是cr ...
- php array 根据value获取key,in_array()判断是否在数组内实例
php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...
随机推荐
- TCP面向连接网络编程
一 TCP&UDP协议 TCP,Tranfer Control Protocol,是一种面向连接的保证可靠传输的协议.通过TCP协议传输,得到的是一个顺序的无差错的数据流.发送方和接收方的成对 ...
- LightSpeed 的Left Join Bug解决方案
在使用LightSpeed对数据库进行Left Join或Right Join操作时,经常会报一些匪夷所思的异常. 明明表没有问题,表面上语句写的也没问题,可总是报错.看分析器里的SQL就知道了,是L ...
- 我的Windows核心编程——完成端口+套接字 图解
========================声明============================ 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本声明). 本文不定期修改完善,为 ...
- cocos2d-x之场景转换特效
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...
- FAQ-Ubuntu12.04 15.04禁止移动介质自动播放
网上有有很多关于Ubuntu10.04关闭移动介质自动播放的方法,包括在文件管理器里面设置或者使用gconf-editor,但是从12.04开始这两种方法都不再好用了,关于移动介质的处理方法被移到了S ...
- libstdc++
一.简介 二.安装 http://www.linuxfromscratch.org/lfs/view/development/chapter05/gcc-libstdc++.html 三.其他 1)l ...
- (转)github设置添加SSH
很多朋友在用github管理项目的时候,都是直接使用https url克隆到本地,当然也有有些人使用 SSH url 克隆到本地.然而,为什么绝大多数人会使用https url克隆呢? 这是因为,使用 ...
- selenium之ExpectedConditions类
API中对于该类的介绍:Canned ExpectedConditions which are generally useful within webdriver tests.很笼统,大概意思就是在w ...
- 【温故而知新-Javascript】窗口效果 (全屏显示窗口、定时关闭窗口)
1.全屏显示窗口 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- 渐进记号 Asymptotic Notations-------geeksforgeeks 翻译
我们已经简单的讨论了下渐进分析以及最坏,平均和最佳情况的分析.渐进分析的主要思想是分析算法的效率,不用依靠计算机的具体快慢,不需要实现这个算法,也不需要真正去计算时间.渐进记号是一种数学的工具来表示渐 ...