Based on Problem One, 8ms ,c++,easily understand


  • 0
    W
    class Solution {
    public:
        int findMin(vector<int>& nums) {
            if(nums.size() == 0) return 0;
            if(nums.size() == 1) return nums[0];
            if(nums[0] < nums[nums.size()-1]) return nums[0];
            return binarySearch(nums,0,nums.size()-1);
        }
        int binarySearch(vector<int>& nums,int start,int end){
            while(start < end && nums[start] == nums[start+1]) start++;
            while(start < end && nums[end] == nums[end-1]) end--;
            int mid = (start+end)/2;
            if((mid == 0 && nums[mid] <= nums[mid+1])||(mid == nums.size()-1 && nums[mid] <= nums[mid-1])||(nums[mid] <= nums[mid+1] &&nums[mid] < nums[mid-1])) return nums[mid];
            if(nums[mid] < nums[end])
            return binarySearch(nums,start,mid-1);
            else return binarySearch(nums,mid+1,end);
            
            
        }
    };

Log in to reply
 

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