题解 | #寻找峰值#
寻找峰值
https://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76
import java.util.*; public class Solution { public int findPeakElement (int[] nums) { int left = 0; int right = nums.length - 1; //二分法 while (left < right) { int mid = (left + right) / 2; //右边是往下,不一定有坡峰 if (nums[mid] > nums[mid + 1]) right = mid; //右边是往上,一定能找到波峰(注意:由于mid偏向于left,所以这里需要+1,否则可能循环) else left = mid + 1; } //其中一个波峰 return right; } }