Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- /**
- * @param A: An integers array.
- * @return: return any of peek positions.
- */
- public int findPeak(int[] A) {
- // write your code here
- int start = 1, end = A.length-2; // 1.答案在之间,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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement