(二分查找 拓展) 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 ...
随机推荐
- MySQL字段操作与数据处理
一,对字段的操作 1.拼接字段:Concat()函数 多数DBMS使用 + 或者 || 来实现拼接,而MySQL使用 Concat() 函数来实现拼接. 实例: Concat()函数拼接时加上的字符需 ...
- 详解块级格式化上下文(BFC)
相信大家和我一样,第一次听到别人说CSS 块级格式化上下文(block formatting context,简称:BFC)的时候一头雾水,为了帮助大家弄清楚块级格式化上下文,我翻阅了W3C的CSS规 ...
- vmware完整克隆(linux)
vmware中的完整克隆是基于指定的虚拟机克隆出相同的一份出来,不必再安装 但是我们要保证三个地方不能一样,一个是主机名称(hostname),一个是虚拟网卡设备mac地址,还有一个是ip地址 所以我 ...
- Salesforce的对象关系
对象关系 Salesforce中的对象关系和一般的关系数据库不同. 在关系数据库中对象间的关系是由主键.外键等加以定义.而在Salesforce中,对象之间的关系是由自定义字段来确定. 这么做的原因是 ...
- 【English】四、Y结尾名词变复数
一.辅音字母+y结尾的名词,将y改变为i,再加-es. 读音变化:加读[z]. 例: candy→candies; daisy→daisies; fairy→fairies; lady→ladies; ...
- HelloHibernate的创建过程
文章提纲 安装与配置 开发小结 建立项目 配置项目 创建代码 执行项目 安装与配置 JDK的安装:建议使用JRE 1.8以上: SQL Server 2000的安装:建议SQL Server 2000 ...
- MongoDB-BSON
概念参考百科说明:BSON( Binary Serialized Document Format) 是一种二进制形式的存储格式,采用了类似于 C 语言结构体的名称.对表示方法,支持内嵌的文档对象和数组 ...
- master公式 ------ 求递归情况下的时间复杂度
剖析递归行为和递归行为时间复杂度的估算一个递归行为的例子T(N) = a*T(N/b) + O(N^d)1) log(b,a) > d -> 复杂度为O(N^log(b,a))2) log ...
- Doctype知识点总结
DOCTYPE是document type (文档类型) 的缩写.<!DOCTYPE >声明位于文档的最前面,处于标签之前,它不是html标签.主要作用是告诉浏览器的解析器使用哪种HTML ...
- 利用 keras_proprecessing.image 扩增自己的遥感数据(多波段)
1.keras 自带的 keras_proprecessing.image 只支持三种模式图片(color_mode in ['grey', 'RGB', 'RGBA'])的随机扩增. 2.遥感数据除 ...