题目描述
思路:同LeetCode852.山脉数组的顶峰索引
注意:当nums数组只有一个元素的时候,这个元素就是顶元素
因为根据题目:nums[-1] = nums[n] = -∞
方法一:
class Solution {
public int findPeakElement(int[] nums) {
if (nums.length == 1) return 0;
int left = 0, right = nums.length - 1;
while (left <= right) {
if (left == right) return left;
int mid = left + (right - left) / 2;
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else if (nums[mid] > nums[mid + 1]) {
right = mid;
}
}
throw new RuntimeException("Error");
}
}
标签:right,nums,int,LeetCode162,峰值,mid,LeetCode
From: https://www.cnblogs.com/keyongkang/p/17918678.html