思路
顺序遍历
public int peakIndexInMountainArray(int[] A) {
for(int i=1;i<A.length-1;i++){
if(A[i]>A[i+1])
return i;
}
return 0;
}
二分查找
public int peakIndexInMountainArray(int[] A) {
int low = 0;
int high = A.length-1;
while(low<=high){
int mid = low + ((high-low)>>1);
if(A[mid]>A[mid+1]&&A[mid]>A[mid-1]){
return mid;
}else if(A[mid]<A[mid+1]&&A[mid]>A[mid-1])
low = mid+1;
else
high = mid -1;
}
return -1;
}