当前位置:网站首页>LeetCode162-寻找峰值-二分-数组
LeetCode162-寻找峰值-二分-数组
2022-04-23 14:48:00 【李烦烦搞快点】
Note:
二分,每次判断一下子是不是峰值,峰值在哪边就好了
不过我们这里条件和变动要统一好,如果我们打算往右边找,就要让他和前一个去比,这样就不会越界
代码如下:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int l = 0, r = nums.size() - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(nums[mid] > nums[mid - 1]) l = mid;
else r = mid - 1;
}
return r;
}
};
版权声明
本文为[李烦烦搞快点]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Mr_Ghost812/article/details/124354942
边栏推荐
- 8.5 循环神经网络简洁实现
- pnpm安装使用
- Programming philosophy - automatic loading, dependency injection and control inversion
- Detailed explanation of C language P2 selection branch statement
- 2-Go变量操作
- 你还不知道责任链模式的使用场景吗?
- ASEMI超快恢复二极管与肖特基二极管可以互换吗
- Master in minutes --- ternary operator (ternary operator)
- 8.4 循环神经网络从零实现
- Don't you know the usage scenario of the responsibility chain model?
猜你喜欢
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
On the insecurity of using scanf in VS
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
阿里研发三面,面试官一套组合拳让我当场懵逼
【Servlet】Servlet 详解(使用+原理)
3、 Gradient descent solution θ
SVN详细使用教程
8.5 循环神经网络简洁实现
随机推荐
ASEMI超快恢复二极管与肖特基二极管可以互换吗
Is asemi ultrafast recovery diode interchangeable with Schottky diode
冰冰学习笔记:一步一步带你实现顺序表
Progress in the treatment of depression
UML项目实例——抖音的UML图描述
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
【NLP】HMM隐马尔可夫+维特比分词
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
PCIe X1 插槽的主要用途是什么?
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
The initial C language framework is suitable for review and preliminary understanding
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
Frame synchronization implementation
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
Contraction mapping theorem
一款不错的工具:aardio
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc