Binary search solution by C


  • 0
    H

    to verify the middle and the head and the tail in one loop

    '''
    int findPeakElement(int* nums, int numsSize) {
    if(numsSize<=1) return 0;

    int left=0;int right=numsSize-1;
    int mid;
    while(left!=right){
        if(nums[left]>nums[left+1]) {
            right=left;break;
        }
        else left+=1;
        if(nums[right]>nums[right-1]){
            left=right;break;
        }
        else right-=1;
        mid=(right-left)/2+left;
        if(nums[mid]<nums[mid+1])left=mid+1;
        else right=mid;
    }
    return left;
    

    }
    '''


Log in to reply
 

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