Cracking The Coding Interview 9.3
来源:互联网 发布:类似知轩藏书 编辑:程序博客网 时间:2024/04/29 01:17
//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;}
0 0
- Cracking The Coding Interview 9.3
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- Cracking the coding interview
- Cracking the coding interview--Q1
- Cracking the coding interview--Q17
- Cracking the coding interview--Q2
- Cracking the coding interview--Q3
- Cracking the coding interview--Q4
- Cracking the coding interview--Q5
- Cracking the coding interview--Q8
- Cracking the coding interview--Q9
- Cracking the coding interview 题目
- Cracking The Coding Interview 1.1
- Cracking The Coding Interview 1.2
- Cracking The Coding Interview 1.3
- 相遇,倾城暖——致友情
- linux/win下安装loadrunner步骤详解
- Oracle补丁集的补丁号Patch ID/Number速查
- [IOS]UIImageView动画
- opencv----计算图像旋转参数
- Cracking The Coding Interview 9.3
- c++ 类中const成员变量的赋值
- 【笔记】C与指针 第七章 函数
- Latex设置表格字体大小
- Cocos2d-x教程(27)-Cocos2d-x 2.2.x tolua++使用方法
- 记录知识点包括ASIHTTPRequest
- 黑马程序员_第四天视频学习 数组 字符串 指针
- 自定义标签库中的tld文件
- android项目源码下载