(二分查找 拓展) 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 nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞
.
Example 1:
Input: nums =[1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input: nums =[
1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
or index number 5 where the peak element is 6.
Note:
Your solution should be in logarithmic complexity.
-----------------------------------------------------------------------------------------------------------------------------------------
这个在leetcode上用O(n)算法可以过,但是在lintcode 上会超时。
可以用二分查找,二分查找有递归写法和迭代写法。
C++:迭代
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == ) return -;
int left = ,right = nums.size() - ;
int mid;
while(left < right){
mid = left + (right - left)/;
if(nums[mid] > nums[mid + ]) right = mid; //mid不会加1,因为它本身也有可能是“山顶”。
else left = mid + ; //会加1 的,因为nums[mid]肯定不是“山顶”,所以忽略它。
}
return left;
}
};
详情见官方题解:https://leetcode.com/articles/find-peak-element/
另一个迭代解法(在lintcode上):
class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> &A) {
// write your code here
if(A.size() == ) return -;
int l = ,r = A.size() - ;
int mid;
while(l < r){
mid = l + (r - l)/;
if(A[mid] < A[mid - ])
r = mid;
else if(A[mid] < A[mid + ])
l = mid + ;
else
return mid;
}
//mid = A[l] > A[r] ? l:r;
//return mid;
}
};
也有一个解法:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == )
return -;
int left = ;
int right = nums.size() - ;
while(left + < right){
int mid = left + (right - left)/;
if(nums[mid] > nums[mid + ]) right = mid;
else if(nums[mid] < nums[mid + ]) left = mid;
//else return mid;
}
int mid = nums[left] > nums[right] ? left:right;
return mid;
}
};
(二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element的更多相关文章
- (二分查找 拓展) 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 69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- (二分查找 拓展) leetcode278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- ✡ 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 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
随机推荐
- webpack中使用DefinePlugin定义全局变量
DefinePlugin可以在编译时期创建全局变量.DefinePlugin是webpack注入全局变量的插件,通常使用该插件来判别代码运行的环境变量.
- 关于图片适配不同尺寸的image View(实战)
分享人:广州华软 佐罗 一. 前言 在前端开发过程中,设计稿中往往只提供一张图片,但是app内需要用到的尺寸各种各样. 同时图片不仅是信息的直接表达,也会为网站起到美观点缀的作用,图片的变形.过分裁切 ...
- Mac电脑 怎么导出安卓手机的相册
1.mac上下载一个HandShaker 2.把电脑和手机设置在同一个wifi下 3.安卓手机上下载一个HandShaker 参考:https://zhidao.baidu.com/question/ ...
- Android为TV端助力:UDP协议(接收组播和单播)
private static String MulticastHost="224.9.9.98";private static int POST=19999;private sta ...
- 在 asp.net core 中使用类似 Application 的服务
在 asp.net core 中使用类似 Application 的服务 Intro 在 asp.net 中,我们可以借助 Application 来保存一些服务器端全局变量,比如说服务器端同时在线的 ...
- deepin linux学习笔记
目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...
- RabbitMQ消息模型概览(简明教程)
小菜最近用到RabbitMQ,由于之前了解过其他消息中间件,算是有些基础,所以随手从网上搜了几篇文章,准备大概了解下RabbitMQ的消息模型,没想到网上文章千篇一律,写一大堆内容,就是说不明白到底怎 ...
- animation动画案例
最近一直苦恼做一个banner的进度条,原先用js改变width值,但明显卡顿.后来用了animation,超级好用. <!DOCTYPE html> <html lang=&quo ...
- 深蓝词库转换2.4版发布,支持最新的搜狗用户词库备份bin格式
很高兴的告诉大家,感谢GitHub上的h4x3rotab提供python版的搜狗用户词库备份bin格式的解析算法,感谢tmxkn1提供了C#版的实现,深蓝词库转换终于迎来了一个重大更新,能够支持搜狗用 ...
- 是时候理解下HTTPS的原理及流程了
1.什么是HTTP协议? HTTP协议是Hyper Text Transfer Protocol(超文本传输协议),位于TCP/IP模型当中的应用层.HTTP协议通过请求/响应的方式,在客户端和服务端 ...