遍历法
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int N = arr.length-1;
int ans = -1;
for(int i = 1;i<N;i++){
if(arr[i]>arr[i+1]){
ans = i;
break;
}
}
return ans;
}
}
二分查找
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int left = 1;
int right = arr.length - 2;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] > arr[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}