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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目2

    2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...

随机推荐

  1. mysqli:查询数据库中,是否存在数据的三种校验方法

    在我们编辑用户登录功能的时候,常常需要对用户输入的信息进行校验,校验的方法就是通过SQL语句进行一个比对,那么我们就需要用到以下三种中的一种进行校验啦 1.使用mysqli_num_rows()校验 ...

  2. Altium_Designer17-PCB-如何重新定义板子外形

    当我们绘制完电路图.完成PCB布局和布线后,我们会遇到下面这种情况: 发现什么了吗?那么大的黑框框,我们只用到了很少的一部分.那有啥?告诉你黑框框的大小就是你最后将要印刷的PCB板的实际大小!大家都知 ...

  3. TCP与虚连接

    http://bbs.csdn.net/topics/390262738 在TCP通信时,会建立一个从源端到目的端的虚拟连接.感觉这种连接类似电路交换,只是这种连接是虚拟存在的.发送的报文都应该是沿着 ...

  4. Poj(2236),简单并查集

    题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...

  5. php图像处理插件imagick安装(仅适用于86位,php5.4非安全环境-16px)

    phpImageMagick-6.7.7-5-Q16-windows-dll(加测试代码,经测试,仅适用于86位,php5.4安全环境-16px) 下载地址:http://pan.baidu.com/ ...

  6. assert函数和捕获异常

    assert函数: C语言和C++都有一个专为调试而准备的工具函数,就是 assert()函数. 这个函数是在C语言的 assert.h 库文件里定义的,所以包含到C++程序里我们用以下语句: #in ...

  7. mingw libgcc_s_sjlj-1.dll is missing

    习惯了在linux环境下工作,编译wingdows平台程序采用mingw工具.编译完,运行exe程序,弹出错误信息: libgcc_s_sjlj-1.dll is missing 百度了一下,原来是编 ...

  8. call()和apply()

    每个函数都包含apply和call方法. 相同点:都接收两个参数,一个是在其中运行函数的作用域,另一个是参数: 不同点:call方法和apply的不同之处在于接收参数的方式不同: apply方法第二个 ...

  9. percona-zabbix-templates插件安装监控MySQL

    前期准备:被监控机已经安装好php 1.在zabbix客户端安装mysql监控插件rpm包 rpm -ivh https://www.percona.com/downloads/percona-mon ...

  10. 用python写一个类似于linux中的tree

    import os filePath = 'g:/File' j = 0 # 查找的深度计数 def tree(filePath,j): dir_now = os.listdir(filePath) ...