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] ≠ ...
随机推荐
- sphinx全文检索之PHP使用教程
以上一篇的email数据表为例: 数据结构: 01.CREATE TABLE email ( 02.emailid mediumint(8) unsigned NOT NULL auto_increm ...
- python之模块:decimal
# -*- coding: utf-8 -*-__author__ = 'Administrator'#数学计算import decimal#用于定点和浮点运算#文档:https://docs.pyt ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- iOS中的界面多选功能--(UICollectionView)
文/Jacob_Pan(简书作者)原文链接:http://www.jianshu.com/p/9d28ebd0f5a2著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 最近做项目接触了一 ...
- linux下安装软件的方法
1. 区分 rpm -qi -qf -ql -qa四个不同选项组合的作用?rpm -qi //查询已经安装的某个RPM软件包的信息rpm -qf //查询某个程序文件是由哪个RPM软件包安装的rpm ...
- document.write 向文档中写内容,包括文本、脚本、元素之类的,但是它在什么时候执行不会覆盖当前页面内容尼?
当你打开一个页面,浏览器会 调用 document.open() 打开文档 document.write(...) 将下载到的网页内容写入文档 所有内容写完了,就调用 document.close() ...
- PL/SQL中字符串变量的分割转化
在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...
- hdu5355 Cake(构造)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Cake Time Limit: 2000/1000 MS (Java/Other ...
- 连接SQLite 创建ADO.net实体类
0.开发环境 win10,vs2013-x64 1.安装: sqlite-netFx451-setup-bundle-x86-2013-1.0.102.0.exe 注意事项:选在VisualStudi ...
- log4net 使用步骤
1.安装log4net 官网首页:http://logging.apache.org/log4net/ 下载地址:http://logging.apache.org/log4net/download_ ...