LeetCode OJ 162. Find Peak Element
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
【题目分析】
在一个数组中任意两个相邻元素是不相同的,找到这样的元素,它比它的邻居节点都要大。如果存在这样的元素,返回其中任意一个即可。
【解体思路】
方法1:
遍历数组,
- 如果是最左边的元素,并且它比右邻居大,那它就是peak elmemnt。
- 如果是最右边的元素,并且它比左邻居大,也是peak element。
- 否则,如果它比左右邻居都大,那么它也是peak element。
代码如下:
class Solution
{
public:
int findPeakElement(vector<int>& nums)
{
if (nums.size() == 1)
{
return 0;
} for (int i = 0; i < nums.size(); i++)
{
if ((i == 0 && nums[i] > nums[i + 1])
|| (i == nums.size() - 1 && nums[i] > nums[i - 1]))
{
return i;
} if (0 < i && i < nums.size() - 1
&& nums[i] > nums[i - 1] && nums[i] > nums[i + 1])
{
return i;
}
} return -1;
}
};
方法2:
上面的算法用到了很多次比较,下面给出一段更简单的代码:
class Solution
{
public:
int findPeakElement(vector<int>& nums)
{
// 对于nums[0]:题目规定它大于左邻居(不存在),因此只要它比右邻居大,那它就是peak element。
// 如果不是,那说明它不比右邻居大。而题目规定相邻节点不相等,因此它必然小于右邻居。
//
// 对于nums[1]:由上面的分析可知:左邻居小于当前节点。因此只要它比右邻居大,那就是peak element了。
// 如果也不是,那说明它也比右邻居小。
// ...
// 对于nums[sz - 2]:如果之前都没有返回。由上面规律可知:它比左邻居大。
// 而按照题目规定,它又一定大于右邻居(不存在)。因此它一定是peak element。
for (int i = 0; i < nums.size() - 1; i++)
{
if (nums[i] > nums[i + 1])
{
return i;
}
}
return nums.size() - 1;
}
};
上面两个解法都能够被LeetCode接受,但是它们的时间复杂度都是是O(n)。而题目中要求是:O(log n)。
方法3:
由上面的分析,我们可以得出这样一个规律:
只要数组满足下面的形式:
[ 小,大,... 未知 ...,大,小 ]
那么其中一定能找到peak elememnt。
尝试用二分法处理:
取数组的中间值mid,
- 如果
mid的值比mid+1的值小那么从
mid开始的数组右半部分将满足:[小,大,...,大,小]的形式,所以其中一定有peak element。又因为
mid肯定不是peak element,所以可以进一步把范围缩小为[mid+1, 数组末尾]。 - 如果
mid的值比mid+1的值大
那么从数组的
开始位置到mid+1位置,也将构成[小,大,...,大,小]的形式。而
mid+1显然也不是peak element。所以可进一步把范围缩小到[数组开始,mid]。
代码如下:
public class Solution {
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length-1;
while(left < right){
int mid = (right - left)/2 + left;
if(nums[mid] > nums[mid+1])
right = mid;
else
left = mid + 1;
}
return left;
}
}
LeetCode OJ 162. Find Peak Element的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【LeetCode】162. Find Peak Element (3 solutions)
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- LeetCode OJ: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
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- leetcode 日记 162. Find Peak Element java python
根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...
- 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 ...
随机推荐
- 中文版的jqGrid实例大全
中文版的jqGrid实例大全 http://blog.mn886.net/jqGrid/
- ajax客户端请求与服务端响应浅谈
AJAX,即Asynchronous Javascript And XML,AJAX本质是在HTTP协议的基础上以异步的方式与服务器进行通信. 所谓的异步,是指某段程序执行不会阻塞其他程序执行,其表现 ...
- js判断数组里面的值是否相等。
var zhi=[]; var zhiT=[]; //var arr=["a","b","a","a"]; var ar ...
- LoRaWAN_stack移植笔记(一)--RF硬件相关
和硬件相关的问题 TCXO 的使用 根据SX1276数据手册, 如果使用TCXO,则需要配置RegTcxo寄存器为0x19,代码如下 ``` c void SX1276SetTcxoConfig(vo ...
- StringMVC(拦截器)
单个拦截器 使用jar包 创建FirstController.java @Controller public class FirstController { @RequestMapping(" ...
- 【1】ShopNC 模仿笔记(一)
不断学习,人生将会成功. 1. 序 一直想模仿一个整套的商城, 今天在shopNC 里面看到了想要的一切, 所以把一些组件, 命名规范等记录下来, 提高以后的开发效率. 官方网站 PC : shopN ...
- 前端 TDD 开发
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px ".PingFang SC"; color: #454545 } p.p2 ...
- tomcat服务配置及搭建
一.在官网上下载tomcat 下载地址:http://tomcat.apache.org/download-60.cgi 下载完后解压 二.设置环境变量 1,JAVA_HOME 2.CATALINA_ ...
- [SQL基础教程] 1-5 表的删除和更新
[SQL基础教程] 1-5 表的删除和更新 表的删除 语法 DROP TABLE <表名>; 法则 1-12 删除的表无法恢复 表定义的更新 语法 ALTER TABLE<表名> ...
- MPMoviePlayerController
属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...