1476. 山形数组的顶峰坐标
我们把符合下列属性的数组 A 称作山脉:
A.length >= 3
存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。
样例
样例 1:
输入: [0,1,0]
输出: 1
样例 2:
输入: [0,2,1,0]
输出: 1
注意事项
1.3 <= A.length <= 10000
2.0 <= A[i] <= 10^6
public class Solution {
/**
* @param A: an array
* @return: any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
*/
public int peakIndexInMountainArray(int[] A) {
int start=0;
int end=A.length-1;
int mid=0;
while (start<end){
mid=(start+end)/2;
if (A[mid]>A[mid-1]&&A[mid]>A[mid+1]){
return mid;
}
if (A[mid]<A[mid-1]){
end=mid;
}
if (A[mid]<A[mid+1]){
start=mid;
}
}
return mid;
}
}