75.Find Peak Element
作者:
博瑜 | 来源:发表于
2017-07-03 08:40 被阅读0次class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
// write your code here
int length = A.length;
int start = 1;
int end = length - 2;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
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]) start = mid;
else end = mid;
}
if (A[start] > A[end]) return start;
else return end;
}
}
本文标题:75.Find Peak Element
本文链接:https://www.haomeiwen.com/subject/uskacxtx.html
网友评论