首页 > 代码库 > ACM && Find Minimum in Rotated Sorted Array

ACM && Find Minimum in Rotated Sorted Array

 1 /*Find Minimum in Rotated Sorted Array */ 2 #include <iostream> 3 #include <vector> 4 using namespace std; 5  6 int find(vector <int>& num, int begin, int end) ; 7 int findMin(vector<int> &num) { 8     cout<<num.size(); 9     return find(num, 0,  num.size()-1 );10 }11 12 int find(vector<int> &num, int begin, int end) {13     cout<<"begin: "<<num[begin]<<"  end: "<<num[end]<<endl;14     if(begin==end)15         return num[end];16     if(begin==end-1)17         return num[begin]>num[end]?num[end]:num[begin];18     if(num[begin]<num[end])19         return num[begin];20     int mid=(begin+end)/2;21     cout<<"mid:"<<num[mid]<<endl;22     if(num[mid]>num[begin])23         return find(num, mid+1, end);24     else if(num[mid]<num[begin])25         return find(num,begin, mid);26     else {27         int a,b;28         a=find(num, begin, mid);29         b=find(num, mid, end);30         return a>b?b:a;31     }32 }33 34 void main(){35     int a[3]={3,1,1};36     vector<int> num(a, a+3);37     int c=findMin(num);38     cout<<c<<endl;39     while(1);40 }

Find Minimum in Rotated Sorted Array II

 

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/

ACM && Find Minimum in Rotated Sorted Array