当前位置:网站首页>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)
边栏推荐
- The earth revolves around the sun
- Answer to "software testing" exercise: Chapter 1
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- 十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
- FairyGUI条子家族(滚动条,滑动条,进度条)
- 国企秋招经验总结
- [algorithm] sword finger offer2 golang interview question 10: subarray with sum K
- 如何保障 MySQL 和 Redis 的数据一致性?
- [untitled]
- Rt-ppp test using rtknavi
猜你喜欢

Fairygui bar subfamily (scroll bar, slider, progress bar)
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组

阿里云一面:并发场景下的底层细节 - 伪共享问题

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

Novatel board oem617d configuration step record

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

MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列

NovAtel 板卡OEM617D配置步骤记录

Halcon knowledge: gray_ Tophat transform and bottom cap transform
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
随机推荐
Devops' future: six trends in 2022 and beyond
[GNSS data processing] Helmert variance component estimation analysis and code implementation
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Knowledge system of digital IT practitioners | software development methods -- agile
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
How to ensure data consistency between MySQL and redis?
NovAtel 板卡OEM617D配置步骤记录
Several high-frequency JVM interview questions
堆排序【手写小根堆】
图书管理系统小练习
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
GNSS positioning accuracy index calculation
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
Heap sort [handwritten small root heap]
[算法] 剑指offer2 golang 面试题1:整数除法
Prove the time complexity of heap sorting
Fairygui bar subfamily (scroll bar, slider, progress bar)
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
Application architecture of large live broadcast platform
Interview Essentials: talk about the various implementations of distributed locks!