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.
Note:
Your solution should be in logarithmic complexity.
因为按照题意,num[0]是大于左边的不存在的那个元素的,num[size-1]也是大于右边那个不存在的元素的,假如不存在,那么就会有num[0]<num[1],num[1]<num[2],就是增序,num[size-2]<num[size-1],这样num[size-1]就是peak elem了,所以一定存在。
于是就是这样的思路,num[NULL] < num[0],我们假设左边的元素小于右边的元素,那么第一个左边元素大于右边的那个一定是peak elem.如num[0].为什么第一个就是呢?因为前面的都是左<右,判断左>右为false。
注意:题目中说了,返回任意一个峰值都行,所以,能用二分搜索,如果要返回第一个峰值的话,二分搜索是不行的。
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left=,right=nums.size()-;
while(left<=right){
if(left==right)
return left;
int mid=(left+right)/;
if(nums[mid]<nums[mid+])
left=mid+;
else
right=mid;
}
}
};
Find Peak Element——二分查找的变形的更多相关文章
- 162. Find Peak Element(二分查找 )
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ...
- leetcode旋转数组查找 二分查找的变形
http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivo ...
- Find Minimum in Rotated Sorted Array I&&II——二分查找的变形
Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...
- Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedM ...
- (二分查找 拓展) 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 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- [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] 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 ...
随机推荐
- 使用Faster-Rcnn进行目标检测(实践篇)转载
原 文链接:http://blog.csdn.net/gavin__zhou/article/details/52052915 原理 上一篇文章,已经说过了,大家可以参考一下,Faster-Rcnn进 ...
- ACE中UDP通信
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585205.html udp是一种无连接的协议,提供无连接不可靠的服务. 在ace中,通 ...
- Android网络请求的时候报错 Connection refused 处理
在用Android测试JSON数据解析的时候,报了这样一个异常: java.net.ConnectException: localhost/ - Connection refused 原来模拟器默认把 ...
- hibernate、mybatis、beetsql的学习
先推荐两篇文章吧: https://my.oschina.net/xiandafu/blog/617542 http://blog.csdn.net/xiandafu/article/details/ ...
- bzoj 3714 [PA2014]Kuglarz 最小生成树
[PA2014]Kuglarz Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1335 Solved: 672[Submit][Status][Di ...
- JQuery学习四(过滤选择器)
:first选择第一个元素.$(“div:first”)进行选择第一个<div> :last 选择最后一个最后一个元素 $("div:last")选取最后一个<d ...
- vue-transition-animation
<!Doctype> <html> <head> <meta charset="utf-8"> <meta name=&quo ...
- Mybatis(1) 创建Mybatis HelloWorld
简介: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...
- 怎么修改jar包中的class文件然后再重新打成jar包
1.导入到eclipse中, 修改完了 然后导出jar即可 2.jar cf file.jar *.class 将当前目录下所有CLASS文件打包成新的JAR文件 3.对于补丁.之前已经有jar 例如 ...
- 【Android】完善Android学习(五:API 3.2)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...