1. 程式人生 > >852. Peak Index in a Mountain Array - Easy

852. Peak Index in a Mountain Array - Easy

用binary search

如果A[mid] < A[mid+1],說明peak在mid右側,l = mid + 1;否則說明peak在mid右側,r = mid

時間:O(N),空間:O(1)

class Solution {
    public int peakIndexInMountainArray(int[] A) {
        int l = 0, r = A.length - 1;
        while(l < r) {
            int mid = l + (r - l) / 2;
            if(A[mid] < A[mid + 1])
                l 
= mid + 1; else r = mid; } return l; } }