https://algorithm.yuanbin.me/zh-hans/binary_search/find_peak_element.html
class Solution {
public:
int findPeakElement(vector<int>& 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;
}
};
网友评论