Minimum number of rotation of the array: prove safety offer8

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.
 
 
C ++ implementation:
{Solution class 
public: 
    int minNumberInRotateArray (Vector <int> rotateArray) { 
        int size = rotateArray.size (); 
        IF (size == 0) { 
            return 0; 
        } 
        IF (size ==. 1) { 
            return rotateArray [0]; 
        } 
        int Front = 0, size =-REAR. 1; 
        
        int = MID Front; 
        the while (rotateArray [Front]> = rotateArray [REAR]) { 
            IF (REAR-== Front. 1) {   // loop exit condition 
                BREAK; 
            } 
            MID = (REAR Front +) / 2; 
            IF (rotateArray [Front] == rotateArray [MID] && rotateArray [MID] == rotateArray [REAR]) { // the head, middle and tail all the same, to be solved one by one traverse minimum
                
                rear= findmin(rotateArray,front,rear);
                break;
            }
            if(rotateArray[mid]>=rotateArray[front]){
                front=mid;
            }else if(rotateArray[mid]<=rotateArray[rear]){
                rear=mid;
            }
            mid=(front+rear)/2;
        }
        return rotateArray[rear];
    }
    int findmin( const vector<int> &rotateArray, int front, int rear){
        int min_pos=front;
        for(int i= front+1;i<=rear;i++){
            if(rotateArray[min_pos]>rotateArray[i]){
                min_pos=i;
            }
        }
        return rotateArray[min_pos];
    }
};                

  

Guess you like

Origin www.cnblogs.com/fancy-li/p/11610314.html