//Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originally sorted in increasing order.
//
//EXAMPLE:
//
//Input: find 5 in array (15 16 19 20 25 1 3 4 5 7 10 14)
//
//Output: 8 (the index of 5 in the array)
#include <iostream>
using namespace std; //原始二分法
//int search(int *a, int begin, int end,int key)
//{
//int mid;
//while(begin < end)
//{
// mid = (begin + end)/2;
// if (a[mid] == key)
// {
// return mid;
// }
// if (key < a[mid])
// {
// end = mid;
// }
// else
// {
// begin = mid;
// }
//} //if (begin < end)
//{
// int mid = (begin + end)/2;
// if (a[mid] == key)
// {
// return mid;
// }
// if (key < a[mid])
// {
// search(a,begin,mid-1,key);
// }
// else
// {
// search(a,mid+1,end,key);
// }
//}
//} int searchInRotate(int *a, int begin, int end,int key)
{ while(begin < end)
{
int mid = (begin + end)/2;
if (a[mid] == key)
{
return mid;
} if (a[mid] >= a[begin])
{
if (key < a[mid] && key >= a[begin])
{
end = mid-1;
}
else
{
begin = mid+1;
}
}
else
{
if (key > a[mid] && key <a[begin])
{
begin = mid + 1;
}
else
{
end = mid -1;
}
} }
} int main()
{
int a[12] = {15,16,19,20,25,1,3,4,5,7,10,14};
//int a[12] = {1,3,4,5,7,10,14,15,16,19,20,25};
//cout<<search(a,0,11,7);
cout<<searchInRotate(a,0,11,25);
return 0;
}

Cracking The Coding Interview 9.3的更多相关文章

  1. Cracking the coding interview

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

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

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

  3. Cracking the Coding Interview(Trees and Graphs)

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

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

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

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

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

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

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

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

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

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. ThinkPHP 日志(如何学好一门技术,教学视频和文档的优缺)

    ThinkPHP 日志(如何学好一门技术,教学视频和文档的优缺) 一.总结 一句话总结:教学视频中介绍的只是基础的最常用的使用,那些不常用的那些视频里面都不会介绍,因为需求小,所以还是需要好好去把参考 ...

  2. (转)c#反射

    1. 什么是反射2. 命名空间与装配件的关系3. 运行期得到类型信息有什么用4. 如何使用反射获取类型5. 如何根据类型来动态创建对象6. 如何获取方法以及动态调用方法7. 动态创建委托 1.什么是反 ...

  3. c# 静态构造函数与私有构造函数共存

    在使用静态构造函数的时候应该注意几点: 1.静态构造函数既没有访问修饰符,也没有参数.因为是.NET调用的,所以像public和private等修饰符就没有意义了. 2.是在创建第一个类实例或任何静态 ...

  4. 输出图片格式BARTENDER

    try {                BarTender.Application btApp = new BarTender.Application();                BarTe ...

  5. 雷林鹏分享:XML 命名空间

    XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 携带 HTML ...

  6. 20165327 2017-2018-2 《Java程序设计》第一周学习总结

    第1章 Java入门 一.Java 的特点 简单 面向对象 平台无关 多线程:允许同时完成多个任务 动态:Java程序的基本组成单元就是类(有些类是自己编写的,有一些是从类库中引入的,而类又是运行时动 ...

  7. C#异步的世界(重点:新异步)

    http://www.cnblogs.com/zhaopei/p/async_two.html

  8. English Voice of <<Bye Bye Bye>>

    Bye Bye Bye - Lovestoned When i see you, looking back at me 当我看到你回首看我时 Watching this eye still 彼此凝视 ...

  9. android--------自定义控件 之 基本实现篇

    前面简单的讲述了Android中自定义控件中的几个方法,今天通过代码来实现一个简单的案例 自定义一个扇形图 自定义控件示例: 这里先介绍继承View的方式为例 public class Circula ...

  10. Bacterial Melee CodeForces - 756D (dp去重)

    大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串. 首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列 并且可以观察到相同 ...