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 ...
随机推荐
- [NOI2017]蔬菜——时光倒流+贪心
题目链接 题解: 貌似一眼看过去是一个贪心. 其他的算法要记录的东西就太多了. 部分分其实很高.但是没有什么提示. 想一些套路:二分?不行还要贪心判断. 分治?前后取法是有影响的. 时光倒流? 也许可 ...
- 【BZOJ 3569】DZY Loves Chinese II 随机化+线性基
用到一个结论——[先建树,再给每个非树边一个权值,每个树边的权值为覆盖他的非树边的权值的异或和,然后如果给出的边存在一个非空子集异或和为0则不连通,否则连通](必须保证每条边的出现和消失只能由自己产生 ...
- Lua Go R HEXO Kotlin 简单介绍
Lua Lua使用C编写而成的脚本语言.同为脚本语言的Python拥有庞大的类库工具包,定位于独立开发,Lua极度精简化,没有提供太多功能包,必须与C.C++等语言混合使用,目的是为了快速并动态的嵌入 ...
- 仅此一文让你明白ASP.NET MVC 之View的显示
有些人要问题,为什么我要学框架?这里我简单说一下,深入理解一个框架,给你带来最直接的好处: 使用框架时,遇到问题可以快速定位,并知道如何解决: 当框架中有些功能用着不爽时,你可以自由扩展,实现你想要的 ...
- Codeforces Round #341 (Div. 2)B
B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Tomcat 映射虚拟目录和程序热部署
虚拟目录的设置 方法一:在${tomcat安装目录}/conf/Catalina/localhost目录下创建一个xml文件,任意文件名都可以,但是此文件名是web应用发布后的虚拟目录: 比如创建一个 ...
- zoj 2006 Glass Beads
Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006 Time Limit: 2 Seconds ...
- LightOJ 1269 - Consecutive Sum Trie树
题意:给出一串序列,求区间连续异或值的最大和最小. 思路:如果不是出在专题里,想不到可以用字典树做.先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或 ...
- 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
[算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...
- 【洛谷 P2783】 有机化学之神偶尔会做作弊 (双联通分量)
题目链接 可能是除了<概率论>的最水的黑题了吧 用\(Tarjan\)缩点(点双联通分量),然后就是树上两点之间的距离了,跑\(LCA\)就好了. #include <cstdio& ...