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 ...
随机推荐
- React获取组件实例
1. 直接new Component() 组件本身也是class,可以new,这样的组件实例意义不大 componentInstance = new Component(); 2. ReactDOM. ...
- 将Visual Studio项目转换为Dot Net Core项目 csproj to xproj
删除csproj文件. 将 package.config 重命名为 project.json . 转换文件,将xml转换为json格式. <?xml version="1.0" ...
- 【枚举】 最大子矩阵(I)
题注:最大子矩形问题的解决办法最初由中国国家集训队王知昆前辈整理并发表为论文,在此说明并感谢. Definition 给你一个大矩形,里面有一些障碍点,求一个面积最大的矩形,满足该矩形在大矩形内部且该 ...
- 理清一下JavaScript面向对象思路
借这篇文章理清一下自己的思路,同时也希望能给和我同样一知半解的同学理清一下思路.引发思考来自于我犯的一个错误,错误代码是这样的: 1 var o = { 2 ... 3 } 4 var obj ...
- CentOS 6.5 下 QT4 连接 mysql 数据库的步骤
QT4 的安装请参考: CentOS 6.5 下安装 QT 4 mysql 的安装请参考: CentOS 6.5 下安装配置 mysql 1. 预防万一,先安装一下mysql-devel(一定要装!) ...
- windows下安装git
1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...
- C++智能指针 auto_ptr
C++智能指针 auto_ptr auto_ptr 是一个轻量级的智能指针, 定义于 memory (非memory.h)中, 命名空间为 std. auto_ptr 适合用来管理生命周期比较短或者不 ...
- sub-G 无线芯片基础知识
1.典型无线收发机编码 2.前导码的作用是使接收机的时钟和发射机同步(有待验证),如果接收机工作在WOR模式,前导码还有唤醒接收机的功能(接收一定数量的前导码),此时发射机必须发送较长的前导码才能把接 ...
- defer与async的区别
当浏览器碰到 script 脚本的时候: <script src="script.js"></script> 没有 defer 或 async,浏览器会立即 ...
- Idea Ant 打开发包
简介: http://ju.outofmemory.cn/entry/19239 语法: https://wenku.baidu.com/view/a0e00315866fb84ae45c8d9d.h ...