Find Peak Element(!!!)

image.png

https://algorithm.yuanbin.me/zh-hans/binary_search/find_peak_element.html

image.png

class Solution {
public:
    int findPeakElement(vector& nums) {
        int l = 0;
        int r = nums.size() - 1;
        while(l + 1 < r){
            int mid = l + (r - l) / 2;
            if(nums[mid] < nums[mid - 1]) r = mid;
            else l = mid;
        }
        int res = nums[l] < nums[r] ? r : l;
        return res;
    }
};

你可能感兴趣的:(Find Peak Element(!!!))