Share my C++ binary search recursion solution


  • 0
    A

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


Log in to reply
 

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