当前位置:网站首页>力扣(LeetCode)162. 寻找峰值(2022.06.11)
力扣(LeetCode)162. 寻找峰值(2022.06.11)
2022-06-12 10:27:00 【ChaoYue_miku】
峰值元素是指其值严格大于左右相邻值的元素。
给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞ 。
你必须实现时间复杂度为 O(log n) 的算法来解决此问题。
示例 1:
输入:nums = [1,2,3,1]
输出:2
解释:3 是峰值元素,你的函数应该返回其索引 2。
示例 2:
输入:nums = [1,2,1,3,5,6,4]
输出:1 或 5
解释:你的函数可以返回索引 1,其峰值元素为 2;
或者返回索引 5, 其峰值元素为 6。
提示:
1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
对于所有有效的 i 都有 nums[i] != nums[i + 1]
通过次数232,819提交次数470,577
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-peak-element
方法一:二分查找
C++提交内容:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
auto get = [=](int i) -> pair<int, int>{
if(i == -1 || i == n){
return {
0, 0};
}else{
return {
1, nums[i]};
}
};
int l = 0, r = n, index = 0;
while(l <= r){
int mid = (l + r) / 2;
if(get(mid - 1) < get(mid) && get(mid) > get(mid + 1)){
index = mid;
break;
}
if(get(mid) < get(mid + 1)){
l = mid + 1;
}else{
r = mid - 1;
}
}
return index;
}
};
边栏推荐
- Amélioration de la 3dsc par HSC
- Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
- PHP: Excel to get the letter header
- Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
- On 3dsc theory and application of 3D shape context feature
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- 4. creator mode
- Valentina Studio Pro for MAC (MAC database management software)
- Error during session start; please check your PHP and/or webserver log file and configure your PHP
- 93. 获得内网的所有IP地址
猜你喜欢

Malicious code analysis practice - lab03-01 Exe basic dynamic analysis

2022京東618預售定金怎麼退?京東618定金能退嗎?
![Reverse analysis of Huawei housekeeper software [transfer]](/img/85/7af372808e75f8791936c59918466f.jpg)
Reverse analysis of Huawei housekeeper software [transfer]

Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?

【MySQL】sql_ Model mode

Chapter 3 search

机器学习不是你想用,想用就能用

Win10 professional edition user name modification

容器江湖的爱恨情仇
随机推荐
The difference between static method locking and non static method locking
高通平台如何修改特殊电压
How Qualcomm platform modifies special voltage
How to upload the video on the computer to the mobile phone without network
golang中的定时器
Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
MySQL implements split method
93. Obtenir toutes les adresses IP de l'Intranet
Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
Mysql5.6.24 installation free deployment method
How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games
93. 获得内网的所有IP地址
Solution to invalid small program scroll into view
Valentina Studio Pro for Mac(mac数据库管理软件)
4. creator mode
XML Parsing Error: mismatched tag. Expected
淺談調和形狀上下文特征HSC對3DSC的改進
机器学习不是你想用,想用就能用
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)