当前位置:网站首页>162. Find peak - binary search
162. Find peak - binary search
2022-07-06 13:05:00 【The_ Dan】
class Solution {
public:
int findPeakElement(vector<int>& nums) {
// Parts must be orderly , When there is no peak , The array must be in ascending or descending order
int n = nums.size();
if(n == 1 || nums[0] > nums[1])
return 0;
if(nums[n - 1] > nums[n - 2])
return n - 1;
int left = 0, right = n - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]){
break;
}
else if(nums[mid] > nums[mid - 1]){
left = mid + 1; // Right will not touch the far right , So you can + 1
}
else{
right = mid; // If it's here - 1, You may encounter the leftmost , Cause the above if Beyond the boundary
}
}
return mid;
}
};
Accepted
63/63 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 63.44 % of cpp submissions (8.6 MB)
边栏推荐
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- Fairygui character status Popup
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
- FairyGUI条子家族(滚动条,滑动条,进度条)
- WSL common commands
- GNSS positioning accuracy index calculation
- 国企秋招经验总结
- How do architects draw system architecture blueprints?
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- Rt-ppp test using rtknavi
猜你喜欢
随机推荐
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Implementation of Excel import and export functions
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
记录:下一不小心写了个递归
[GNSS data processing] Helmert variance component estimation analysis and code implementation
[算法] 剑指offer2 golang 面试题1:整数除法
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
wsl常用命令
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
VLSM variable length subnet mask partition tips
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
[GNSS] robust estimation (robust estimation) principle and program implementation
[Chongqing Guangdong education] Shandong University College Physics reference materials
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
继承和多态(下)
121 distributed interview questions and answers
Usage differences between isempty and isblank
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce