There is a integer array which has the following features:
- The numbers in adjacent positions is different.
- A[0] < a[1] && a[a.length-2] > a[a.length-1].
We define a position P is a peek if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in the this array. Return the index of the peak.
Given[1, 2, 1, 3, 4, 5, 7, 6]
Return index 1 (which is number 2) or 6 (which is number 7)
classSolution {/** * @parama:an integers array. * @return: Return any of peek positions. */ Public intFindpeak (int[] A) {//Write your code here if(A = =NULL|| A.length = = 0) return0; intleft = 1; intright = A.length-2; while(Left < Right-1){ intMid = left + (right-left)/2; if(A[left] <= A[mid] && A[mid] <=A[right]) left=mid; Else if(A[left] >= A[mid] && A[mid] >=A[right]) right=mid; Else if(A[left] <= A[mid] && A[right] >=A[mid]) right--; Else Left++; } if(A[left] >A[right])returnLeft ; Else returnRight ; }}
Lintcode-medium-find Peak Element