(leetcode162)find peak element
1题目
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.
2分析
使用分治策略。
3 源代码
public int findPeakElement(int[] num) {
int low = 0;
int high = num.length-1; while(low < high)
{
int mid1 = (low+high)/2;
int mid2 = mid1+1;
if(num[mid1] < num[mid2])
low = mid2;
else
high = mid1;
}
return low;
}
(leetcode162)find peak element的更多相关文章
- LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- [Swift]LeetCode162. 寻找峰值 | Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [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 ...
随机推荐
- LibreOJ #6008. 「网络流 24 题」餐巾计划 最小费用最大流 建图
#6008. 「网络流 24 题」餐巾计划 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- R及Rstudio 的使用建议
对于新人来说,进行R的学习时,通常会发现一般的教程都是让大家在交互环境下使用R. 但是这有一些缺点,比如在交换环境下,出现错误是难以撤销的,有的时候甚至需要重头做起.尤其是在Rstudio的交互环境下 ...
- ECLIPSE使用HG插件去上载 GOOGLE.CODE下的代码
ECLIPSE使用HG插件去上载 GOOGLE.CODE下的代码 www.MyException.Cn 发布于:2012-09-10 22:20:12 浏览:112次 0 ECLIPSE使 ...
- mybatis学习六 parameterType 属性
1. 在 XXXMapper.xml 中<select><delete>等标签的 parameterType 可以控制参数类型2. SqlSession 的 selectLis ...
- Alpha 冲刺 (4/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前后端接口的开发 测试项目运行的服务器环 ...
- innodb_log_buffer_size和innodb_buffer_pool_size参数说明
innodb_log_buffer_size Command-Line Format --innodb_log_buffer_size=# System Variable Name innodb ...
- 2019.01.01 bzoj3625:小朋友和二叉树(生成函数+多项式求逆+多项式开方)
传送门 codeforces传送门codeforces传送门codeforces传送门 生成函数好题. 卡场差评至今未过 题意简述:nnn个点的二叉树,每个点的权值KaTeX parse error: ...
- 2018.10.30 uoj#273. 【清华集训2016】你的生命已如风中残烛(组合数学)
传送门 组合数学妙题. 我们把这mmm个数都减去111. 然后出牌的地方就变成了−1-1−1. 然后发现求出每个位置的前缀和之后全部都是非负数. 考虑在最后加入一个−1-1−1构成一个m+1m+1m+ ...
- ajax +jsp+iframe无刷新上传文件[转]
http://hi.baidu.com/zj360202/blog/item/f23e3711f929c774cb80c475.html ajax jsp 无刷新上传文件 2009-10-26 16: ...
- poj-3928(树状数组)
题目链接:传送门 题意:n个乒乓球运动员要互相练习,都去一个运动员那里比赛,举办训练的运动员不能水平最高或最低. 现在给出n个运动员的水平,求出最终有多少种组合. 思路:先对运动员进行离散化,然后进行 ...