2020-08-01

852. 山脉数组的峰顶索引

二分法,如果当前a[m+1] > a[m], 那么m肯定不是峰顶, l = m+1, 否则 m+1肯定在峰顶右边 r = m;

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& A) {
        int l = 0 , r = A.size()-1;
        while(l<r){
            int m = (l+r)>>1;
            if(A[m+1] > A[m]) l = m+1;
            else r = m;
        }
        return l;
    }
};

 

posted @ 2020-08-01 12:56  樱花庄的龙之介大人  阅读(84)  评论(0编辑  收藏  举报