Straightforward Java recursive solution,O(logN)


  • 0
    Y
    public int findPeakElement(int[] nums) {
        return findIt(nums,0,nums.length-1);
    }
    public int findIt(int[] nums, int start, int end){
        int mid = (start+end)/2;
        if(mid==start) return nums[start]>nums[end]?start:end;
        if(nums[mid-1]<nums[mid]&&nums[mid]>nums[mid+1]) return mid;
        if(nums[mid-1]>nums[mid]) return findIt(nums,start,mid-1);
        else return findIt(nums,mid+1,end);
    }

  • 0
    L
    This post is deleted!

Log in to reply
 

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