力扣162.寻找峰值

class Solution {
    public int findPeakElement(int[] nums) {
        int len = nums.length;
        if (len == 1) {
            return 0;
        }
        if (nums[0] > nums[1]) {
            return 0;
        }
        if (nums[len - 1] > nums[len - 2]) {
            return len - 1;
        }

        int left = 1, right = len - 2,m = 0, res = -1;

        while (left <= right) {
            m = left + ((right-left) >> 1);
            if (nums[m-1] > nums[m]) {
                right = m - 1;
            } else if (nums[m + 1] > nums[m]) {
                left = m + 1;
            }else{
                res = m;
                break;
            }
        }
        return res;
    }
}

你可能感兴趣的:(算法与数据结构入门(java),算法,排序算法,leetcode,java)