二分法找部分有序数组题
class Solution { public int findPeakElement(int[] nums) { int left=0; int right=nums.length-1; while(left<right){//因为这道题需要用mid和mid+1比较,所以左右不可以相等否则mid+1会越界 int mid=left+(right-left)/2; if(nums[mid]>nums[mid+1]){//如果mid比mid+1大,说明递减,峰值在左边,移动右端 right=mid; }else{ left=mid+1;//反之,峰值在右边,由于mid已经比mid+1小了,所以mid不可能是峰值,左端移动到mid+1 }} return right; } 标签:right,int,mid,峰值,二分法,lc162 From: https://www.cnblogs.com/doudou666/p/18128896