C++ solution 6 ms and beat 100%


  • 1
    A

    this is my solution:


  • 1
    A

    class Solution {
    public:

    int find( vector<int>& nums, int left, int right )
    {
        if ( left+1==right && nums[left]>=nums[right] )  return nums[right];
        if ( nums[left]<nums[right] )  return nums[left];
        
        int mid=(left+right)>>1;
        if ( nums[mid]>nums[left] )    
            return find( nums, mid, right );
        else if ( nums[mid]<nums[left] )
            return find( nums, left, mid );
        else
            return find( nums, left+1, right );
    }
    
    
    int findMin(vector<int>& nums) {
        
        if ( nums.size()==1 ) return nums[0];
        int size = nums.size()-1;
        return find( nums, 0, size );
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.