LeetCode:162 寻找峰值

class Solution {
    public int findPeakElement(int[] nums) {
       
        if(nums.length==1||nums[0]>nums[1]){
            return 0;
        }
        if(nums[nums.length-1]>nums[nums.length-2]){
            return nums.length-1;
        }
        int left=0;
        int right = nums.length-1;
        int mid=0;
        while(left<=right){
            mid = (left + right)>>1;

            if(nums[mid]>nums[mid+1]){
                right = mid-1;
            }
            else{
                left = mid +1;
            }
        }
        return left;
    }
}

 

posted @ 2020-10-17 15:44  dlooooo  阅读(67)  评论(0编辑  收藏  举报