BS code(LogN times)


  • 0
    C
    class Solution {
    public:
    int findPeakElement(vector<int>& nums) {
    
    	int n = nums.size();
    	if (n == 0 || n == 1)
    		return 0;
    	return helper(nums, 0, n - 1);
    }
    private:
    int helper(vector<int>& nums, int s, int e)
    {
    	int mid = s + (e - s) / 2;
    
    
    	if (mid+1 > e || nums[mid] > nums[mid + 1])
    	{
    		if (mid-1 < 0 || nums[mid] > nums[mid - 1])
    			return mid;
    		else
    			return helper(nums, s, mid - 1);
    	}
    	else
    		return helper(nums, mid + 1, e);
    }
    };

  • 0
    C
    This post is deleted!

Log in to reply
 

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