The title describes moving a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sequence that outputs 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. Analysis: The time complexity of the method used for this problem is O (N), if the time complexity of using the binary lookup method is O (LgN). The binary lookup method first defines two pointers to the head and tail of the array, compares the middle element to the first element, and if the intermediate element is greater than the first element, the smallest element is in the following paragraph. Note that the special case of the problem handles 1,0,1,1,1,1/1,1,1,1,0,1 and 1,1,1,1,0 in cases where the middle element and the first element have the last element equal, only right--.
classSolution { Public: intMinnumberinrotatearray (vector<int>Rotatearray) {//can not be removed!!! There has to be a legitimacy check . if(rotatearray.size () = =0){ return 0; }/*int min = rotatearray[0]; for (int i = 0; i < rotatearray.size (); i++) {if (min > rotatearray[i]) min = rotatearray[ I]; } return min;*/ intleft =0, right = Rotatearray.size ()-1; if(Rotatearray[left] <Rotatearray[right])returnRotatearray[left]; while(Left <Right ) {//if (Rotatearray[left] < Rotatearray[rotatearray.size ()/2]) intMid = (left + right) >>1; if(Rotatearray[mid] >Rotatearray[left])//Left = Rotatearray.size ()/2;left =mid; Else if(Rotatearray[mid] <Rotatearray[left])//Right = Rotatearray.size ()/2;right =mid; Else { if(Rotatearray[left] = =Rotatearray[right]) right--; Else Left=mid; } if(Right-left = =1) returnRotatearray[right]; } returnRotatearray[right]; }};
Minimum number of rotated array