Cracking The Coding Interview 9.3
//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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- linux下逻辑卷管理 调整分区大小
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50 ...
- Spring 拦截器实现+后台原理(HandlerInterceptor)
过滤器跟拦截器的区别 spring mvc的拦截器是只拦截controller而不拦截jsp,html 页面文件的.这就用到过滤器filter了,filter是在servlet前执行的,你也可以理解成 ...
- Transcranial magnetic stimulation (TMS)
Transcranial magnetic stimulation (TMS) Effect of Transcranial Magnetic Stimulation on Free Will Tra ...
- Twisted 简介
Twisted是用Python实现的基于事件驱动的网络引擎框架,Twisted支持许多常见的传输及应用层协议,包括TCP.UDP.SSL/TLS.HTTP.IMAP.SSH.IRC以及FTP.就像Py ...
- OPSF - 2,状态机
1,报文更新地址 点到点:所有报文发送224.0.0.5 虚链路:单播地址 广播网络上:DR OTHER至DR/BDR 224.0.0.6,DR/BDR至DR OTEHER 2 ...
- LeetCode--443--压缩字符串(未看)
问题描述: 给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长 ...
- Composer 的学习
一.Composer简介 Composer 是PHP用来管理依赖关系的工具. 使用 composer 的必要前提有: 1.PHP版本要高于PHP5.3.2 2.PHP支持OpenSSL扩展 3.安装有 ...
- 『TensorFlow Internals』笔记_源码结构
零.资料集合 知乎专栏:Bob学步 知乎提问:如何高效的学习 TensorFlow 代码?. 大佬刘光聪(Github,简书) 开源书:TensorFlow Internals,强烈推荐(本博客参考书 ...
- 创建springboot的聚合工程(二)
前篇已经成功创建了springboot的聚合工程并成功访问,下面就要开始子工程木块之间的调用: springboot项目的特点,一个工程下面的类必须要放在启动类下面的子目录下面,否则,启动的时候会报错 ...