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 ...
随机推荐
- Ruby 基础教程 第一部分总结
第一部分:Ruby 初体验 第一章: Ruby 初探 前言 开头的这一章节讲了一些十分基础的内容,重要的几个话题有: ruby 命令的执行方法 对象.方法的概念 常见的打印方法 ruby 命令的执行方 ...
- Laravel中APP_KEY起什么作用
框架中是这样描述的: This key is used by the Illuminate encrypter service and should be set to a random, 32 ch ...
- Linux下查看相应端口的进程
1)查找被占用的端口:netstat -tln | grep 7777 2)查看被占用端口的PID:lsof -i:7777 3)禁用使用kill -9 PID来禁用端口进程
- 【JS】【2】ajax传的参数为数组时,后台接收为null的处理
前言: 1,参考博客:解决JavaScript中使用$.ajax方式提交数组参数 - Just_Do - 博客园(http://www.cnblogs.com/caoyc/p/5710702.html ...
- 纯css实现顶部进度条随滚动条滚动
<!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...
- thinkphp3.1.3导入
1.首先我们做导入一定要在我们的项目中导入一个 PHPExcel 下载地址:http://phpexcel.codeplex.com/ 2.下载之后就解压我们就可以看到像这样子的文件 3.就 ...
- ireport部署到Linux服务器上遇到的问题解决
ireport报表在本地Windows环境运行正常,一旦部署到Linux环境上出现了如下问题: 1.打开报表,后台直接报net.sf.jasperreports.engine.util.JRFontN ...
- Leetcode 1013. 总持续时间可被 60 整除的歌曲
1013. 总持续时间可被 60 整除的歌曲 显示英文描述 我的提交返回竞赛 用户通过次数450 用户尝试次数595 通过次数456 提交次数1236 题目难度Easy 在歌曲列表中,第 i 首 ...
- CRM UI 打印PDF
这是项目上看到的绝技^_^ 1.画SF.这步就不说了 2.确定参数,写SF打印PDF函数 FUNCTION zsrv_print_to_pdf . *"------------------- ...
- Failed to stop iptables.service: Unit iptables.service not loaded.解决方法
CentOS7中执行 service iptables start/stop 会报错Failed to start iptables.service: Unit iptables.service fa ...