思路
- 根据
[mid-1]
[mid]
[mid+1]
之间的大小来确定答案所在的区间 - 再在指定区间上二分
- 画图,peak一定是
[mid-1]
和[mid+1]
到它都是上升的趋势 - 上升的趋势在哪边,答案就在哪边
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int l = 0;
int r = n-1;
while(l < r){
int mid = l + r + 1 >> 1;
if(nums[mid] >= nums[mid-1])
l = mid;
else
r = mid - 1;
}
return l;
}
}