当前位置:网站首页>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)
边栏推荐
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- WSL common commands
- Application architecture of large live broadcast platform
- The port is occupied because the service is not shut down normally
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- VLSM variable length subnet mask partition tips
- Employment of cashier [differential constraint]
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- Realization of the code for calculating the mean square error of GPS Height Fitting
猜你喜欢
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
阿里云微服务(四) Service Mesh综述以及实例Istio
Redis介绍与使用
Fairygui loop list
图书管理系统小练习
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
How do architects draw system architecture blueprints?
一文搞定 UDP 和 TCP 高频面试题!
Rt-ppp test using rtknavi
音乐播放(Toggle && PlayerPrefs)
随机推荐
isEmpty 和 isBlank 的用法区别
Several high-frequency JVM interview questions
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
[算法] 劍指offer2 golang 面試題2:二進制加法
服务未正常关闭导致端口被占用
如何保障 MySQL 和 Redis 的数据一致性?
记录:Navicat Premium初次无法连接数据库MySQL之解决
基于rtklib源码进行片上移植的思路分享
Error: sorting and subscript out of bounds
PRIDE-PPPAR源码解析
Lean product development - Lean Software Development & lean product development
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
How to improve the deletion speed of sequential class containers?
Wechat applet development experience
[Chongqing Guangdong education] Shandong University College Physics reference materials
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
FGUI工程打包发布&导入Unity&将UI显示出来的方式
平衡二叉树详解 通俗易懂
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
WSL common commands