剑指offer--面试题11:旋转数组的最小数字
2017-07-07 20:07
781 查看
#include<stdio.h>
#include<iostream>
using namespace std;
int MinInOrder(int *a,int low,int high)
{
int result=a[low];
for(int i=low+1;i<=high&&a[i]<result;++i)
result=a[i];
return result;
}
int Min(int *a,int length)
{
if(a==NULL||length<=0)
throw new std::exception("Invalid parameters");
for(int low=0,high=length-1,mid=low;a[low]>=a[high];)
{
if(high-low==1)
return a[high];
mid=(low+high)/2;
if(a[low]==a[high]&&a[mid]==a[low])
return MinInOrder(a,low,high);
if(a[mid]>=a[low])
low=mid;
if(a[mid]<=a[high])
high=mid;
}
}
void main()
{
int a[6]={3,4,5,6,1,2};
printf("旋转数组{3,4,5,6,1,2}的最小数字是:%d\n",Min(a,6));
int b[7]={1,2,0,1,1,1,1};
printf("旋转数组{1,2,0,1,1,1,1}的最小数字是:%d\n",Min(b,6));
printf("空指针测试:%d\n",Min(NULL,0));
}
相关文章推荐
- 【剑指offer】面试题 11:旋转数组的最小数字
- 【剑指offer】面试题11:旋转数组的最小数字
- 剑指offer 面试题8—旋转数组的最小数字
- 【剑指Offer学习】【面试题8 : 旋转数组的最小数字】
- 【剑指offer】面试题八:旋转数组的最小数字
- 剑指offer-面试题8-旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组的最小数字
- 剑指Offer---面试题8:旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 剑指offer面试题[8]-旋转数组的最小数字
- 【剑指Offer面试题】 九度OJ1386:旋转数组的最小数字
- [剑指offer][面试题08]求旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组的最小数字
- 旋转数组的最小数字(剑指offer面试题)
- 【剑指offer】 面试题8: 旋转数组的最小数字
- 【剑指offer】2.4.1查找和排序——面试题8:旋转数组的最小数字
- 剑指offer-面试题8-旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组中的最小数字
- 剑指offer——面试题8:旋转数组的最小数字