//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. spring集成JMS访问ActiveMQ

    首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记 步骤: 在pom.xml中加上需要的包 修改web.xml,增加IOC容器 spring配置文件applicat ...

  2. PHP数组排序函数有哪些

    PHP数组排序函数有哪些 一.总结 一句话总结:其实也就是是否保持索引,对键排序还是对值排序,除了sort,rsort.shuffle,usort外,都是保持索引的 是否保持索引 键排序 值排序 除了 ...

  3. VS2008版本引入第三方dll无强签名

    sn.exe 和ilasm.exe 是系统自带程序.如果显示无此命令,可以从“我的电脑”直接搜索. 将dll文件放入目录下,用VS开发人员命令执行以下命令即可.(以Interop.Scripting. ...

  4. 新C# 操作Excel属性

    C# 操作Excel属性 数字(Range.NumberFormatlocal 属性) 常规:Range.NumberFormatlocal = "G/通用格式" 数值:Range ...

  5. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点

    jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点 通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree ...

  6. liunx系统部署

    Linux系统安装与基本配置 =======================================内容提要:获取Linux 常用发行版的方式DELL/HP/IBM 服务器介绍DELL/HP/ ...

  7. 20170912xlVBA批量导入txt文件

    Public Sub BatchImportTextFiles() AppSettings 'On Error GoTo ErrHandler Dim StartTime, UsedTime As V ...

  8. Confluence 6 空间权限和链接到相关的空间

    空间权限 每一个空间将会创建一个默认的权限.创建空间的用户将会自动具有空间管理员(space admin)的权限,这个的意思是你可以为其他用户和用户组赋予空间访问和管理的权限. 请查看 Space P ...

  9. 迷宫最短路径问题的dfs,bfs实现

    迷宫的最短路径 给定一个大小为 N×M的迷宫.迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动.请求出从起点到终点所需的小步数.请注意,本题假定从起点一定可以移动到终点 限制条件:N,M ...

  10. Selenium-WebDriverApi接口详解

    浏览器操作 # 刷新 driver.refresh() # 前进 driver.forward() # 后退 driver.back() 获取标签元素 # 通过ID定位目标元素 driver.find ...