Find Peak Element 解答
Question
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.
Your solution should be in logarithmic complexity.
Solution
The assumption is a very good hint. It assures that there must be a peak in input array.
We can solve this problem by Binary Search.
Four situations to consider:
1. nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]
=> mid is a peak
2. nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]
=> There must exists a peak in right side
3. nums[mid] < nums[mid - 1] && nums[mid] > nums[mid + 1]
=> There must exists a peak in left side
4. nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]
=> Either in right or left side, there must exists a peak.
public class Solution {
public int findPeakElement(int[] nums) {
// Binary Search to find peak
int start = 0, end = nums.length - 1, mid = 0, prev = 0, next = 0;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
prev = mid - 1;
next = mid + 1;
if (nums[mid] > nums[prev] && nums[mid] > nums[next])
return mid;
if (nums[mid] > nums[prev] && nums[mid] < nums[next]) {
start = mid;
continue;
}
if (nums[mid] < nums[prev] && nums[mid] > nums[next]) {
end = mid;
continue;
}
start = mid;
}
if (nums[start] > nums[end])
return start;
return end;
}
}
Find Peak Element 解答的更多相关文章
- [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 ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- Java for 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
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- 162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- PHP页面静态化(转)
在很多地方都看到有PHP整站静态化的东东,怪唬人的..其实,你会静态化一个页面,那么别说整站了,想静态化多少都可以.所以关键是,首先要知道怎么静态化一个页面,了解静态化的原理是关键.. 这里就说下我个 ...
- html5+css3中的background: -moz-linear-gradient 用法 (转载)
转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...
- EF中的约定
优先级:Fluent API >数据注释>约定 CodeFirst约定 主键约定 如果类的属性名为"ID"(不区分大小写)或类名的后面跟有"ID", ...
- Parser Error Message: Access is denied【转】
PRB: Access Denied Error When You Make Code Modifications with Index Services Running View products ...
- Access restriction: The type * is not accessible due to restrict,报错问题
解决方案1: Eclipse 默认把这些受访问限制的API设成了ERROR. Windows -> Preferences -> Java -> Compiler -> Er ...
- DataSet与DataAdapter的关系
DataSet 作用:DataSet,DataAdapter读取数据. 问:什么是DataAdapter?答:DataAdapter对象在DataSet与数据之间起桥梁作用 string strCon ...
- PHP 日期格式化 参数参考
a - "am" 或是 "pm" A - "AM" 或是 "PM" d - 几日,二位数字,若不足二位则前面补零; 如: ...
- (三)backbone - API学习 - v0.9.2 与 v1.1.2区别
Backbone.View v0.9.2 中Backbone.View 可以导出对象的options属性, v1.1.2 中去掉该属性,通过如下代码 viewOptions = ['model', ' ...
- OpenGL ES 2.0 曲面物体的构建
球体构建的基本原理构建曲面物体最重要的就是找到将曲面恰当拆分成三角形的策略. 最基本的策略是首先按照一定的规则将物体按行和列两个方向进行拆分,这时就可以得到很多的小四边形.然后再将每个小四边形拆分成两 ...
- 引用 exit、return、_exit、_Exit这几个函数的区别
引用 exit.return._exit._Exit这几个函数的区别 一.exit函数和return函数的主要区别是: exit用于在程序运行的过程中随时结束程序,其参数是返回给OS的.也可以这么讲: ...