My clean Java code


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

Log in to reply
 

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