leetcode852 C++ 20ms 找最高峰 序列先增后减

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& A) {
        int res=0;
        for(int i=0;i<A.size();i++){
            if(A[i] > A[res]){
                res=i;
            }
        }
        return res;
    }
};


class Solution {
public:
    int rec(vector<int>& A, int left, int right){
        int mid = (left + right) / 2;
        if(mid == 0 || mid == A.size()-2){
            if(A[mid] > A[mid+1]){
                return mid;
            }
            else{
                return mid+1;
            }
        }
        else{
            if(A[mid] > A[mid-1] && A[mid] > A[mid+1]){
                return mid;
            }
            else if(A[mid] > A[mid-1]){
                return rec(A, mid, right);
            }
            else if(A[mid] < A[mid-1]){
                return rec(A, left, mid-1);
            }
        }
    }
    int peakIndexInMountainArray(vector<int>& A) {
        return rec(A, 0, A.size()-1);
    }
};



posted @ 2018-08-10 17:40  一条图图犬  阅读(382)  评论(0编辑  收藏  举报