consider both ends first


  • 0
    N
        if(nums.length==1){
            return 0;
        }
        if(nums[0]>nums[1]){
            return 0;
        }
        if(nums[nums.length-1]>nums[nums.length-2]){
            return nums.length-1;
        }
        
        
        int lo = 0;
        int hi = nums.length-1;
        
        while(lo<hi-1){
            int mid = (hi-lo)/2+lo;
            if(nums[mid]>nums[mid+1] && nums[mid]>nums[mid-1]){
                return mid;
            }
            else if(nums[mid]<nums[mid+1]){
                lo = mid+1;
            }
            else{
                hi = mid-1;
            }
        }
        return nums[lo]>nums[hi]? lo : hi;

Log in to reply
 

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