Cpp simple bisection search


  • 0
    P
    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int n = nums.size();
            if(n<=1) return 0;
            if(nums[0]>nums[1]) return 0;
            if(nums[n-1]>nums[n-2]) return n-1;
            int a = 0, b = n-1;
            while(a<b-1){
                int c = (a+b)/2;
                if(nums[c]>nums[c+1] && nums[c]>nums[c-1]) return c;
                if(nums[c]<nums[c+1]) a = c;
                else b = c;
            }
            return b;
        }
    };
    

Log in to reply
 

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