No.292 - LeetCode[852] Peak Index in a Mountain Array - 二分山峰数组

/*
 * @lc app=leetcode id=852 lang=cpp
 *
 * [852] Peak Index in a Mountain Array
 */

// @lc code=start
class Solution {
public:
    void bsearch(int L, int R, vector<int>& arr, int& loc, int& mx){
        if(L+1 >= R){
            if(arr[L] > mx){
                mx = arr[L];
                loc = L;
            }
            return ;
        }
        int mid = (L+R)/2;
        if(arr[mid] > arr[R-1]){
            bsearch(L, mid+1, arr, loc, mx);
        }
        if(arr[mid] > arr[L]) {
            bsearch(mid, R, arr, loc, mx);
        }
    }
    int peakIndexInMountainArray(vector<int>& arr) {
        int N = arr.size();
        int loc = 0;
        int mx = 0;
        bsearch(0, N, arr, loc, mx);
        return loc;
    }
};
// @lc code=end
上一篇:对接验证码短信接口-RUBY语言


下一篇:Ruby 的 super 怎么用