ADD Date 2014-10-15
Find Minimum in rotated Sorted Array
Suppose a sorted array is rotated on 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.
Assume no duplicate exists in the array.
This is very simple, because there is no repeating number, the array is essentially ordered, and the method of finding with similar binary complexity O (log n). Code is not a few lines, look directly at code, remember to consider the entire array is not rotated situation.
1 classSolution {2 Public:3 intFindmin (vector<int> &num,intPreintPost) {4 if(Pre = =Post)5 returnNum[pre];6 if(Num[pre] <Num[post])7 returnNum[pre];8 intMid = (pre+post)/2;9 if(Num[mid] >=Num[pre])Ten returnFindmin (num, mid+1, post); One if(Num[mid] <Num[post]) A returnfindmin (NUM, pre, mid); - } - intFindmin (vector<int> &num) { the intLen =num.size (); - returnFindmin (NUM,0, len-1); - } -};
Continue to brush leetcode tomorrow.
"Leetcode" Find Minimum in rotated Sorted array finding the smallest number in a rotating array