1.题目:
2.解析:
暴力遍历代码:O(N),由于该题数据很少所以可以通过
暴力遍历:O(N),由于该题数据很少所以可以通过
int index = 0;
for(int i = 1; i <= nums.length-1; i++) {
//某段区域内一直递增,更新就index
if(nums[i] > nums[index]) index = i;
}
//找到并且返回
return index;
二分查找:时间复杂度为 O(log n)
int left = 0, right = nums.length-1;
while(left < right){
int mid = left + (right - left) / 2;
if(nums[mid] > nums[mid+1]) right = mid;
if(nums[mid] < nums[mid+1]) left = mid+1;
}
return left;