《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55
题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位。找出其中是否存在某一个值。
解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移量的二分搜索。两个过程都是对数级的。
代码:
// 11.3 Given a sorted array rotated by a few positions, find out if a value exists in the array.
// Suppose all elements in the array are unique.
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std; int rotatedBinarySearch(vector<int> &v, int n, int key)
{
int offset; if ((int)v.size() < n || n <= ) {
return -;
} int ll, rr, mm; if (v[] < v[n - ]) {
offset = ;
} else {
ll = ;
rr = n - ;
while (rr - ll > ) {
mm = (ll + rr) / ;
if (v[mm] > v[ll]) {
ll = mm;
} else {
rr = mm;
}
}
offset = rr;
} ll = ;
rr = n - ;
while (ll <= rr) {
mm = (ll + rr) / ;
if (key < v[(mm + offset) % n]) {
rr = mm - ;
} else if (key > v[(mm + offset) % n]) {
ll = mm + ;
} else {
return (mm + offset) % n;
}
}
return -;
} int main()
{
int n;
int i;
vector<int> v; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
scanf("%d", &i);
printf("%d\n", rotatedBinarySearch(v, n, i));
} return ;
}
解法2:如果数组的元素可能存在重复,那么我的思路仍然是先二分查找找出偏移量,然后执行带偏移量的二分搜索。不过,在找偏移量的过程中可能会出现无法决定向左还是向右的情况,比如这两个例子{1, 3, 1, 1, 1}{1, 1, 1, 3, 1},在第一次二分时,左中右的元素都是‘1’,无法确定应该往哪边走。这时就得扫描整段,{1, 1, 1}全部是同一元素,{1, 3, 1}存在不同元素,所以应该选择{1, 3, 1}进行二分,因为在首尾相同的情况下,中间如果有不同元素的话,表示旋转的偏移量应该会落在这个区间里。找到偏移量以后,之后的查找就是严格二分的了。
代码:
// 11.3 Given a sorted array rotated by a few positions, find out if a value exists in the array.
// Suppose the array may contain duplicates, what's it gonna be then?
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std; int rotatedBinarySearch(vector<int> &v, int n, int key)
{
int offset; if ((int)v.size() < n || n <= ) {
return -;
} int ll, rr, mm;
int i; ll = ;
rr = n - ;
while (rr - ll > && v[ll] == v[rr]) {
mm = (ll + rr) / ;
if (v[mm] > v[ll]) {
ll = mm;
break;
} else if (v[mm] < v[ll]) {
rr = mm;
break;
} else {
for (i = ll; i < mm - ; ++i) {
if (v[i] != v[i + ]) {
break;
}
}
if (i < mm - ) {
rr = mm;
break;
}
for (i = mm; i < rr - ; ++i) {
if (v[i] != v[i + ]) {
break;
}
}
if (i < rr - ) {
break;
} // if all elements are the same, it ends here
return (v[] == key) ? : -;
}
} if (v[ll] < v[rr]) {
offset = ;
} else {
// here it is guaranteed v[ll] != v[rr]
while (rr - ll > ) {
mm = (ll + rr) / ;
if (v[mm] >= v[ll]) {
ll = mm;
} else {
rr = mm;
}
}
offset = rr;
} // the binary search part remains the same, difference lies in how we find the 'offset'.
ll = ;
rr = n - ;
while (ll <= rr) {
mm = (ll + rr) / ;
if (key < v[(mm + offset) % n]) {
rr = mm - ;
} else if (key > v[(mm + offset) % n]) {
ll = mm + ;
} else {
return (mm + offset) % n;
}
} return -;
} int main()
{
int n;
int i;
vector<int> v; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
scanf("%d", &i);
printf("%d\n", rotatedBinarySearch(v, n, i));
} return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目3的更多相关文章
- 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章:排序和搜索——题目2
2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...
随机推荐
- mysql 5.6 zip安装,启动失败,1067错误
在使用mysql5.6 zip压缩包安装mysql过程中,启动过程,老是卡在1067启动错误上,翻看网上各种解决方案,卸载干净重装,重启,都不管用. 网上各种教程都是新建 my.ini mysql 配 ...
- avast从隔离区恢复后,仍无法打开被误杀文件的解决方案
从隔离区中手动恢复后,隔离区中被恢复的文件将不再展示. 此时,如果手动恢复的文件仍无法打开(图标此时也异常),请: 将avast禁用: 将avast启用. 然后尝试重新打开被误隔离并手动恢复的文件.
- log4net 简单配置
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...
- NutDao配置多数据源
首先,我必须声明,这是一个非常简单的方法,很多小菜没做出来,是因为把nutz想得太复杂 数据源(或者是数据库连接池),在Nutz.Ioc看来,是一个普通的Bean,没任何特别之处. 再强调一点,除了$ ...
- bzoj1150 [CTSC2007]数据备份
Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中 ...
- 2019.03.15 ZJOI2019模拟赛 解题报告
得分: \(20+45+15=80\)(三题暴力全写挂...) \(T1\):Lyk Love painting 首先,不难想到二分答案然后\(DP\)验证. 设当前需验证的答案为\(x\),则一个暴 ...
- Python类型转换+序列操作+基本概念辨析速查手册
第一部分是Python语言中基础中的基础,根据网上资料,合并如下: 1.类型转换 int(x [,base]) 将x转换为一个整数 long(x [,base]) 将x ...
- React后台管理系统-品类的增加、修改和查看
1.页面 2.品类列表展示 let listBody = this.state.list.map((category, index) => { return ( ...
- 【Java】重载(Overload)与重写(Override)
方法的语法 修饰符 返回值类型 方法名(参数类型 参数名){ ... 方法体 ... return 返回值; } 重载(overload) /** * 重载Overload: * 同一个类中,多个方法 ...
- eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet& ...