//============================================================================
// Name : reversestring.cpp
// Author : qxyu
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<unordered_map>
//#include<uint32_t>
using namespace std;
/**
* Definition for a point.
*/
int min_order(vector<int>& input, int p, int q){
int min = input[0];
for(int i = p; i < q; i ++)
{
if(min>input[i])
min=input[i];
}
return min;
}
int find_min(vector<int>& input, int p, int q){
if(input.size()<=0||p>q)
throw new exception("Invalid parameters");
if(p==q-1) return input[q];
int mid = p+(q-p)/2;
if(input[p]==input[q]&&input[p]==input[mid])
return min_order(input, p, q);
if(input[mid]>=input[p])
{
p=mid;
return find_min(input,p,q);
}
else if(input[mid]<=input[q])
{
q=mid;
return find_min(input,p,q);
}
}
int main()
{
vector<int> pre;
pre.push_back(3);
pre.push_back(4);
pre.push_back(5);
pre.push_back(6);
pre.push_back(8);
pre.push_back(1);
pre.push_back(2);
pre.push_back(3);
vector<int> in;
in.push_back(1);
in.push_back(0);
in.push_back(1);
in.push_back(1);
in.push_back(1);
cout<<find_min(pre,0,7)<<" "<<find_min(in,0,4);
return 0;
}
找旋转递增数组最小值
最新推荐文章于 2019-12-18 16:06:15 发布