Title Description
The beginning of an array of several elements moved to the end of the array, the array we call rotation. A non-descending order of the input array of a rotation of the output rotary smallest element array. For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum. NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.
analysis
Binary search.
Code
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.empty())
return 0;
int low = 0;
int high = rotateArray.size() - 1;
int mid = 0;
while(low < high) {
if(rotateArray[low] < rotateArray[high])
return rotateArray[low];
mid = low + (high - low) / 2;
if(rotateArray[low] < rotateArray[mid])
low = mid + 1;
else if(rotateArray[mid] < rotateArray[high])
high = mid;
else
low++;
}
return rotateArray[low];
}
};