当前位置:网站首页>BM19 寻找峰值
BM19 寻找峰值
2022-06-21 16:05:00 【程序员·小李】
给定一个长度为n的数组nums,请你找到峰值并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个所在位置即可。

public int findPeakElement (int[] nums) {
// write code here
if (nums == null || nums.length == 0){
return -1;
}
int i = 0;
while (i <= nums.length - 1){
boolean left = false;
boolean right = false;
if (i - 1 < 0 || nums[i] > nums[i-1]){
left = true;
}
if (i + 1 > nums.length - 1 || nums[i] > nums[i+1]){
right = true;
}
if (left && right){
return i;
}
i++;
}
return -1;
}注:直接判断nums[i] > nums[i-1] && nums[i] > nums[i+1]可能会漏掉只有一个或两个的情形。
附加条件:
1.峰值元素是指其值严格大于左右相邻值的元素。严格大于即不能有等于
2.假设 nums[-1] = nums[n] = −∞
3.对于所有有效的 i 都有 nums[i] != nums[i + 1]
4.你可以使用O(logN)的时间复杂度实现此问题吗?
使用二分法查找的原理是:
利用二分法,若存在某一位置nums[i] < nums[i+1]时,则继续向右查找,右侧必定有极值。
(因为左侧mid~mid+1已经完成了一次升序,那么,右侧只要有一个降序,就出现了极值,若右侧是下图绿色的值,那么mid+1就是极值,如果是红色的,继续向右查找,最迟直到最后一个元素,必然是极限值,为啥?一直向右找,说明左侧递增,而nums[n] = −∞,所以必然满足)左侧同理。

public int findPeakElement (int[] nums) {
// write code here
if (nums == null || nums.length == 0){
return -1;
}
int low = 0;
int high = nums.length - 1;
while (low < high){
int mid = low + (high - low) / 2;
if (nums[mid] < nums[mid + 1]){
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
边栏推荐
- Kotlin DSL构建
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- StarkRocks 第二讲 基本操作(1)
- Detailed explanation of Fisher information quantity detection countermeasure sample code
- Variables and pointers
- 「运维有小邓」Active Directory批量修改用户
- Sorting out Android kotlin generic knowledge points
- [MySQL learning notes 13] comprehensive exercise of query statements
- 我的小工具-卡片学习APP 完成啦
- Uni app framework learning notes
猜你喜欢

Qt 知识:使用 QGraphicsPixmapItem类
![[MySQL learning notes 19] multi table query](/img/e6/4cfb8772e14bc82c4fdc9ad313e58a.png)
[MySQL learning notes 19] multi table query

第八章 可编程接口芯片及应用【微机原理】

【mysql学习笔记17】常用函数整理

Still using xshell? Try this cool SSH terminal tool, which is very powerful!

How can aggressive programmers improve R & D efficiency Live broadcast Preview

Beaucoup de sociétés de logiciels sont en fait des "blagues"

室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?

大话内存四区

Stack growth direction and memory growth direction
随机推荐
Many software companies are actually "jokes"
[MySQL learning notes 19] multi table query
第八章 可编程接口芯片及应用【微机原理】
Jetpack Compose 管理状态(一)
类、接口、函数
The main relations and differences between Poisson sampling and Bernoulli sampling
Use picgo core and Alibaba cloud to automatically upload typera pictures
Kotlin常用函数 let,with,apply,also,run
Integrated base scheme presentation
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
新增Razor组件支持代理连接RDP,JumpServer堡垒机v2.23.0发布
Set up your own website (11)
【mysql学习笔记15】用户管理
【mysql学习笔记12】分页查询
How can aggressive programmers improve R & D efficiency Live broadcast Preview
path. join() 、path. Basename() and path extname()
QT knowledge: using the qgraphicspixmapitem class
Detailed explanation of Fisher information quantity detection countermeasure sample code
The new razor component supports proxy connection to RDP, and jumpserver fortress v2.23.0 is released
用Node创建一个服务器