当前位置:网站首页>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)
边栏推荐
- MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
- Record: I accidentally wrote a recursion next time
- Record: Navicat premium can't connect to MySQL for the first time
- 【无标题】
- 错误:排序与角标越界
- [algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
- How to reduce the shutdown time of InnoDB database?
- [算法] 剑指offer2 golang 面试题2:二进制加法
- Redis介绍与使用
- All in one 1405: sum and product of prime numbers
猜你喜欢

几道高频的JVM面试题
![[算法] 剑指offer2 golang 面试题2:二进制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 剑指offer2 golang 面试题2:二进制加法

Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche

rtklib单点定位spp使用抗差估计遇到的问题及解决

Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing

十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
![[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现

Fgui project packaging and Publishing & importing unity & the way to display the UI

Chromatic judgement bipartite graph
随机推荐
Detailed explanation of balanced binary tree is easy to understand
Fairygui character status Popup
121道分布式面试题和答案
Experience summary of autumn recruitment of state-owned enterprises
【无标题】
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
Shortest Hamilton path (pressure DP)
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
FairyGUI循環列錶
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Interview Essentials: talk about the various implementations of distributed locks!
错误:排序与角标越界
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
编辑距离(多源BFS)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
使用rtknavi进行RT-PPP测试
[Chongqing Guangdong education] Shandong University College Physics reference materials