Comment on page

162.Find-Peak-Element

162. Find Peak Element

题目地址

题目描述

There is an integer array which has the following features:
​
The numbers in adjacent positions are different.
A[0] < A[1] && A[A.length - 2] > A[A.length - 1].
We define a position P is a peak if:
​
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.

代码

public class Solution {
​
int start = 1, end = A.length - 2;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (A[mid] < A[mid - 1]) {
end = mid;
} else if (A[mid] < A[mid + 1] ) {
start = mid;
} else {
end = mid;
}
}
​
if (A[start] < A[end]) {
return end;
} else {
return start;
}
​
}