题目链接
思路
一个不严谨但是好理解的思路是:如果 \(nums[mid] > nums[mid + 1]\),那么 \(nums[mid + 1]\) 肯定不是峰值,此时让 \(right = mid\),从左边继续找峰值。反之则 \(nums[mid]\) 肯定不为峰值,让 \(left = mid + 1\)。
代码
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int left = 0;
int right = n - 1;
while(left < right){
int mid = (right - left) / 2 + left;
if(nums[mid] > nums[mid + 1]){
right = mid;
}else{
left = mid + 1;
}
}
return left;
}
}
标签:二分,right,nums,int,mid,峰值,LeetCode,162,left
From: https://www.cnblogs.com/shixuanliu/p/17396813.html