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. 解决Zend加密的PHP页面出现Incompatible file format的问题

    错误如图: 致命错误:不兼容的文件格式可能的原因: 1.文件本身加密的问题,很有可能你用的Zend进行加密了,但是因为版本的问题,很有可能是你的主机上的Zend Optimizer版本太低了.2.文件 ...

  2. mysql索引和正确使用方式

    一.索引类型 B树索引:大部分都是,因此B树的特性限制了索引如何使用:必须看看索引的正确使用限制(含组合索引的限制)http://blog.csdn.net/lovemdx/article/detai ...

  3. jQuery实现网页右下角悬浮层提示

    最近有同事提到类似网页右下角的消息悬浮提示框的制作.我之前也做过一个类似的例子,很简单.是仿QQ消息.现在感觉之前的那个例子只是说了实现原理,整体上给你的感觉还是太丑,今天为大家带来一个新的例子.是D ...

  4. 20145238-荆玉茗 《Java程序设计》第3周学习总结

    20145238 <Java程序设计>第3周学习总结 教材学习内容总结 一.定义类: ·类定义时使用class关键字 ·如果要将x绑定到新建的对象上,可以使用"="制定 ...

  5. C#浏览器中在线操作文档

    源码地址:https://github.com/SeaLee02/FunctionModule   文件夹 UploadFiles/WebDemo/COM/OnlineEdit.aspx 就是源码 用 ...

  6. 插入数据返回自增id及插入更新二合一

    原文https://blog.csdn.net/dumzp13/article/details/50984413 JDBC: con.setAutoCommit(false); String sql ...

  7. java基础 java中枚举的应用 抽象方法问题

    package com.swift.meiju; import org.junit.Test; public class Demo{ @Test public void test() { System ...

  8. Lucene检索提高性能的几个方式

    1.采用最新版本的Lucene 2.索引文件存储采用本地文件系统,如果需要挂载远程系统,请采用 readonly方式. 3.当然采用更好的硬件,更高I/O的磁盘 4.提高OS 缓存,调整参数 5.提高 ...

  9. 微信小程序日期选择器

    /* JS代码部分 */ const date = new Date() const years = [] const months = [] const days = [] const hours ...

  10. CBCGPImage的GetSize的问题及解决方法

    BCGControlBar Pro for MFC 25.10是目前(2018-07-16)网上能够找到的最新能够使用的版本,我配合Visual Studio 2010使用.在单文档MFC程序的视图中 ...