[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 ...
随机推荐
- CentOS6.5+nginx+tomcat负载均衡集群
思路: 1.安装jdk 2.安装tomcat 3.安装nginx依赖库 4.安装nginx 5.nginx+2个tomcat集群 一:网络拓扑
- 【mysql】一个关于order by排序的问题
I have a table CREATE TABLE `tableMain` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value1` varchar(45) ...
- POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取
第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...
- 17082 两个有序数序列中找第k小
17082 两个有序数序列中找第k小 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 无限制 Description 已知两个已经排好序(非减 ...
- python module getopt usage
import getopt import sys def usage(): print 'this is a usage.' def main(): try: print sys.argv #sys. ...
- ubuntu 添加启动器
终于搞定了安卓开发环境,不知道折腾了多少次,多少个IDE,解决了一个问题,又冒出一个问题.烦死了,最后关头,都快放弃了,重启电脑,打开 android stuio 编译运行居然陈宫了,没有报错,why ...
- 最小的k个数
// 最小的k个数.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include & ...
- Mathout 安装部署
安装Mahout,并运行测试样例,抓图测试实验过程 证明已部署成功 Mahout 下载地址:http://apache.dataguru.cn/mahout/0.9/mahout-distributi ...
- [转]Ionic + AngularJS angular-translate 国际化本地化解决方案
本文转自:http://www.cnblogs.com/maoyazhi/p/4332362.html 欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术 ...
- 怎样用好ZBrush中的PaintStop插件
PaintStop是ZBrush®3.1的手绘插件,可以比较真实的模拟手绘风格,尤其是用水彩笔刷画水墨风格画.PaintStop插件可供用户免费使用. PaintStop是一款功能强大的插件,已经被添 ...