Title Description:
Moves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. Note: All elements given are greater than 0, and if the array size is 0, return 0.
Analysis:
The deformation of the binary lookup. Slowly narrow the search interval until you find it.
Code:
1 classSolution {2 Public:3 intMinnumberinrotatearray (vector<int>Rotatearray) {4 intLen =rotatearray.size ();5 if(Len = =0)return 0;6 intleft =0, right = Len-1, mid;7 while(Left <=Right ) {8Mid = (left + right) >>1;9 intLeftnum =Rotatearray[left];Ten intRightnum =Rotatearray[right]; One intMidnum =Rotatearray[mid]; A if(Leftnum < Rightnum)returnLeftnum; - if(Midnum >rightnum) -Left = mid +1; the Else if(Midnum <rightnum) -right =mid; - Else { -left++; +right--; - } + } A returnRotatearray[mid]; at } -};
The smallest number of offer--rotating arrays