Advertisement

(搜索)定位峰值点

阅读量:

题目

在这里插入图片描述

方法一:线性查找

复制代码
    public int findPeakElement(int[] nums) {
        int len = nums.length;
        if(len == 1)
            return 0;
        int first, second, third ;
        for(int i = 0; i < len; i++){
            first = i == 0 ? Integer.MIN_VALUE : nums[i-1];
            second = nums[i];
            third = i == len - 1 ? Integer.MIN_VALUE : nums[i+1];
            if(first < second && second > third)
                return i;
        }
        return -1;
    }
    

全部评论 (0)

还没有任何评论哟~