LeetCode——162. Find Peak Element
一.题目链接:
https://leetcode.com/problems/find-peak-element/
二.题目大意:
给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局部最大值),并返回该极大值的下标,并假设 nums[-1] = nums[n] = -∞.;当某元素同时大于它两边的元素时,则该元素是数组中的一个极大值,数组中若存在多个极大值,则返回任意一个即可。
算法的时间复杂度要求为log级别。
三.题解:
这个问题的最直观的解法,就是遍历一遍数组,然后判断每个元素是否符合极大值的条件,但是时间复杂度为O(N),不符合题目的要求。既然要求是对数级别的时间复杂度,那就不妨利用二分法的思想来查找符合条件的元素。
代码如下:
class Solution
{
public:
int findPeakElement(vector<int>& nums)
{
if(nums.empty())
return -1;
int len = nums.size();
int low = 0;
int high = len - 1;
while(low < high - 1)//至少要省出一个中间值来,所以low < high -1
{
int mid = ((high - low) >> 1) + low;//一位运算符的优先级比较低,所以一定加上括号
if(nums[mid] > nums[mid + 1] && nums[mid] > nums[mid - 1])
return mid;
else if(nums[mid] < nums[mid + 1])
{
if (low == mid)//特殊情况,必须通过low = mid + 1跳出这种循环
low = mid + 1;
else
low = mid;
} else
high = mid;
}
return nums[low] > nums[high] ? low : high;//有可能还没找到mid就跳出循环了,此时low和high必有一个满足条件 }
};
该算法的时间复杂度为O(logN),空间复杂度为O(1),能够满足题目的要求。有几点需要注意:
1.循环的终止条件为low < high -1,因为极大值要同时大于两边的元素,所以至少要有三个值,否则就跳出循环。
2.有种特殊情况,就是low = mid后,再次更新mid的话,low与mid的值还是相同,相当于陷入了死循环,此时通过low = mid + 1跳出死循环。
3.如果在循环中没有返回mid的话,可能数组只有一个元素,也可能只有数组的末尾元素才是极大值,所以最后要判断一下low和high的值,并返回其中较大的。
四.题目扩展:
如果数组为二维数组的话,如何找到局部最大值(极大值)?
一般有三种方法:直接遍历、利用二分法、利用画圈法(分治)。具体可以参考:https://www.jianshu.com/p/b4f5cb071f04 或者https://blog.csdn.net/m0_37747541/article/details/79629457
LeetCode——162. Find Peak Element的更多相关文章
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ✡ leetcode 162. Find Peak Element --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- leetcode 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- ajax请求,函数外无法获取请求的数据问题解决
一.开发中遇到需要通过ajax请求获取其他函数能否执行的状态,但是当赋值给statusFlag时发现无法赋值:ajax请求默认为异步的方式,该请求的操作被放置在任务队列中,并不会按顺序执行,所以被赋值 ...
- Archlinux配置~小米笔记本Air 13.3英寸版本
1 .zsh echo $ SHELL \\查看当前正在使用shell: pacman -S zsh zsh-syntax-highlighting git wget wget https://raw ...
- React native中DrawerNavigator,StackNavigator,TabNavigator导航栏使用
import React from 'react'; import { View, Text,Button } from 'react-native'; import { DrawerNavigato ...
- java生成jar并用ikvm生成dll供C#调用
最近想尝试用C#做NB_IOT北向API接口的访问.北向API的接口的访问需要证书的双向认证,而C#不支持双向认证,所以就使用IKVM在C#中跑JAVA程序实现HTTPS请求部分. 步骤如下: 一.使 ...
- 部署springboot工程到linux上及遇到的坑
一.步骤 1.将工程打成jar包 IDEA中点击file-project structure,如下图: 选择Artifacts-JAR-From modules,选择入口类Main Class,如下图 ...
- c指针存放的是常量的地址
''' #include <stdio.h> int main(){ int b = 1; int c = 2; int* a = &b; printf(& ...
- 30天代码day0
a class is a collection of variables (fields) and functions called methods. A program is a collectio ...
- adb 安装安卓包
1.搭建安卓环境,或者下载安装ADB工具 2.adb version检查是否安装成功 3.用数据线连上手机,并在手机中打开USB调试模式,使用adb devices 查看链接的设备 这样表示成功连接上 ...
- springCloud、springBoot学习
http://projects.spring.io/spring-cloud/ 官网https://springcloud.cc/spring-cloud-netflix.htmlhttp://cl ...
- hadoop MapReduce
简单介绍 官方给出的介绍是hadoop MR是一个用于轻松编写以一种可靠的.容错的方式在商业化硬件上的大型集群上并行处理大量数据的应用程序的软件框架. MR任务通常会先把输入的数据集切分成独立的块(可 ...