二分查找(C++实现)

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {

        int n = arr.size();

        if(n==0)
        {
            return -1;
        }

        int left = 0;
        int right = n-1;

        while(left<right)
        {
            int mid = left + (right-left)/2;

            if(arr[mid]<arr[mid+1])
            {
                left = mid + 1;
            }

            else{

                right = mid;
            }
        }
        return left;
    }
};

 

 

posted @ 2020-11-03 12:32  诗和远方*  阅读(175)  评论(0编辑  收藏  举报