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. 利用Excel导入数据到SAP C4C

    假设要导入一个Account数据到C4C系统. 工作中心Data Workbench,工作中心视图Import,点download metadata: 会下载一个压缩包到本地. 进入文件夹Templa ...

  2. IOS 获取.plist文件的数据

      @property (nonatomic,strong) NSArray *apps; //获取.plist数据 /**获取plist文件的数组数据*/ -(NSArray *)apps{ if( ...

  3. NutDao配置多数据源

    首先,我必须声明,这是一个非常简单的方法,很多小菜没做出来,是因为把nutz想得太复杂 数据源(或者是数据库连接池),在Nutz.Ioc看来,是一个普通的Bean,没任何特别之处. 再强调一点,除了$ ...

  4. 【洛谷2605】[ZJOI2010] 基站选址(线段树维护DP)

    点此看题面 大致题意: 有\(n\)个村庄,每个村庄有\(4\)个属性:\(D_i\)表示与村庄\(1\)的距离,\(C_i\)表示建立基站的费用,\(S_i\)表示能将其覆盖的建基站范围,\(W_i ...

  5. Uva 11401 数三角形

    题目链接:https://uva.onlinejudge.org/external/114/11401.pdf 题意:1~n个数里面挑3个不同的数,组成一个三角形.求方案数. 分析: 令最长的边为X, ...

  6. 矩阵连乘的相乘次数(ZOJ1094)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=94 解题报告: 1.数据结构: ///矩阵行数和列数 struct N ...

  7. 百度非会员满速下载利器(IDM)Internet Download Manager v6.30.8 中文特别版

    下载利器(IDM)Internet Download Manager v6.30.8 中文特别版 所属分类:工具软件 应用平台:Windows 资源版本:v6.30.8 最后更新:2018年04月14 ...

  8. MapReduce计算每年最大值测试样例生成程序

    Demo.java package com.java; import java.io.BufferedWriter; import java.io.File; import java.io.FileW ...

  9. C# 复合赋值操作符

    前面讲过如何使用算术操作符来创建新值.例如,以下语句使用操作符+来创建比变量answer大42的一个值,新值将写入控制台: Console.WriteLine(answer + 42); 前面还讲过如 ...

  10. 13、SpringBoot------整合shiro

    开发工具:STS 前言: shiro,一套简单灵活的安全权限管理框架. 把所有对外暴露的服务API都看作是一种资源,那么shiro就是负责控制哪些可以获得资源,哪些不能获取. 一个比较不错的教程:ht ...