一定要早日上岸鸭 · July 22, 2021 0

162. 寻找峰值

162. 寻找峰值

class Solution { // logn 首先考虑二分: // 中点右边大于中点则峰值在右边 // 中点右边小于中点则峰值在左边 public boolean biggerleftright(int[] nums, int idx){ return nums[idx]>nums[idx+1] ? true : false ; } public int findPeakElement(int[] nums) { int l = 0, r = nums.length-1; while(l<r){ int mid = l+r>>1; if(biggerleftright(nums, mid)) r = mid; else l = mid + 1; } return l; } }