Question on Find peak


  • 0
    Q

    I feel the nums[-1] and nums[n] shall not be assumed as -oo, otherwise, there would be such meaningless solution:

    public class Solution {
            public int findPeakElement(int[] nums) {
                if (nums == null || nums.length == 0) {
                    return -1;
                }
                for (int i = 1; i < nums.length; ++i) {
                    if (nums[i] < nums[i-1]) {
                        return i - 1;
                    }
                }
                return nums.length-1;
            }
        }

Log in to reply
 

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