当前位置:网站首页>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)
边栏推荐
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- Wechat applet development experience
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- 微信小程序开发心得
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
- Interview Essentials: talk about the various implementations of distributed locks!
- 地球围绕太阳转
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
猜你喜欢
![Heap sort [handwritten small root heap]](/img/f0/6efda3c6f499a32671a935dd2f21db.png)
Heap sort [handwritten small root heap]

Basic DOS commands
![[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal](/img/11/ee0628a68542236fc641966579a31a.png)
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
![[algorithm] sword finger offer2 golang interview question 10: subarray with sum K](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K

121 distributed interview questions and answers

Fairygui loop list

How to ensure data consistency between MySQL and redis?
![[算法] 劍指offer2 golang 面試題2:二進制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 劍指offer2 golang 面試題2:二進制加法

The port is occupied because the service is not shut down normally

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
随机推荐
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
Answer to "software testing" exercise: Chapter 1
一文搞定 UDP 和 TCP 高频面试题!
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
How to ensure data consistency between MySQL and redis?
雇佣收银员【差分约束】
记录:newInstance()过时的代替方法
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
如何保障 MySQL 和 Redis 的数据一致性?
[算法] 剑指offer2 golang 面试题1:整数除法
rtklib单点定位spp使用抗差估计遇到的问题及解决
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
MySQL shutdown is slow
Code example of MATLAB reading GNSS observation value o file
[algorithm] sword finger offer2 golang interview question 2: binary addition
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
FairyGUI循環列錶
Employment of cashier [differential constraint]