2014-03-20 03:01

题目:给定一个已按升序排序的数组,找出是否有A[i] = i的情况出现。

解法1:如果元素不重复,是可以严格二分查找的。

代码:

 // 9.3 Given a unique sorted array, find a position where A[i] = i, if one exists.
#include <cstdio>
#include <vector>
using namespace std; int main()
{
vector<int> v;
int n;
int i;
int ll, rr, mm; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
if (v[] > || v[n - ] < n - ) {
mm = -;
} else {
ll = ;
rr = n - ;
while (ll <= rr) {
mm = (ll + rr) / ;
if (v[mm] < mm) {
ll = mm + ;
} else if (v[mm] > mm) {
rr = mm - ;
} else {
break;
}
}
if (ll > rr) {
mm = -;
}
}
printf("%d\n", mm); v.clear();
} return ;
}

解法2:如果元素可能存在重复,那么会出现无法确定答案在左边还是右边的时候。比如{-1, 3, 3, 3, 3},后面的连续4个‘3’中有A[i]<i,有A[i]>i,也有A[i]=i的。这种情况下就得两边都进行查找了。在没有重复的时候,还是可以二分的,因此总体复杂度介于O(log(n))和O(n)之间,依数据好坏而变。

代码:

 // 9.3 Given a sorted array, find a position where A[i] = i, if one exists. The array may have duplicates.
#include <cstdio>
#include <vector>
using namespace std; int findMagicIndex(vector<int> &v, int start, int end)
{
if (start > end || start > (int)v.size() - || end < ) {
return -;
}
int mid = (start + end) / ;
if (v[mid] == mid) {
return mid;
} int res = findMagicIndex(v, start, (mid - < v[mid] ? mid - : v[mid]));
if (res >= ) {
return res;
}
res = findMagicIndex(v, (mid + > v[mid] ? mid + : v[mid]), end);
return res;
} int main()
{
vector<int> v;
int n;
int i; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
printf("%d\n", findMagicIndex(v, , n - )); v.clear();
} return ;
}

《Cracking the Coding Interview》——第9章:递归和动态规划——题目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(Trees and Graphs)

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

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

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

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

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

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

  8. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11

    2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...

  9. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10

    2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...

  10. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9

    2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...

随机推荐

  1. 深搜,四色定理,(POJ1129)

    题目链接:http://poj.org/problem?id=1129 解题报告: #include<iostream> #include<cstdio> #include&l ...

  2. WPF中TreeView单击展开其子元素以及点击一个元素展开其他元素收起

    TreeView单击展开其子元素: 在WPF的TreeView控件中,要想展开它的子元素,我们必须要鼠标左键点两下或者右键点一下,那么我们怎样实现左键点一下就使它展开呢? Xaml: <Grid ...

  3. ffmpeg处理RTMP流媒体的命令大全

    最近浏览国外网站时候发现,翻译不准确的敬请谅解. 1.将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/li ...

  4. 用dynamic的方式来转换Json对象

    来自这里:http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object If you ...

  5. android build.prop详解

    # begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过设置形成系统信息} ro.build.id=MIUI(版本ID) ...

  6. matlab 下载

    Obrazy DVD programu Matlab ke stažení Verze 2015b Verze 2015a Verze 2014b Verze 2014a Verze 2012b Ve ...

  7. linux flushing file system caches

    We may drop the file system caches on Linux to free up memory for applications. Kernels 2.6.16 and n ...

  8. ajax实现无刷新两级联动DropDownList

    ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...

  9. 面试-Spring理解

    转自http://hui.sohu.com/infonews/article/6331404387079946240 spring呢,是pivotal公司维护的一系列开源工具的总称,最为人所知的是sp ...

  10. XML 对xml文件的crud的增加 create操作 增加元素 增加属性

    把创建的节点挂到上一节点的最后 找到参考节点,使用insertBefore方法进行插入位置 xml添加属性使用setAttribute方法