(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 ...
随机推荐
- libpcap 库使用(一)
参考资料: http://www.tcpdump.org/ DESCRIPTION The Packet Capture library provides a high level interface ...
- [Robot Framework] 执行时报 webdriver 异常
在用Robot Framework通过Selenium2Library做web界面自动化测试的时候,报webdriver的错误: 此种情况是因为WebDriver的版本与浏览器的版本不对应. WebD ...
- Spring ApplicationContext(十)finishRefresh
ApplicationContext(十)finishRefresh Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 经过 ...
- python的杨辉三角
# # / \ # # / \ / \ # # / \ / \ / \ # # / \ / \ / \ / \ # # / \ / \ / \ / \ / \ # # ---------------- ...
- 设置frameset高度
设置frameset的高度 设置frameset高度 目前做了一个项目,界面如下: 这是使用frameset做的,在宽屏下开发一直没有发现什么问题,直到一个用户使用800*600的机子测试的时候, ...
- Windows-universal-samples学习笔记系列四:Data
Data Blobs Compression Content indexer Form validation (HTML) IndexedDB Logging Serializing and dese ...
- MySQL open_files_limit相关设置
背景: 数据库链接不上,报错: root@localhost:/var/log/mysql# mysql -uzjy -p -h192.168.1.111 --default-charact ...
- ubuntu16下的/etc/resolv.conf重置的解决方案
此文件存放了网络网关信息,重启后会刷新,刷新来源有两个可能 一个是根据文件中的resolvconf目录下的resolv.conf.d目录下的base文件 另一个来源是/etc/network/inte ...
- IDEA导入MySQL包
点击[Project Structure] 点击[Modules] 在点击下面的界面 找到自己下载的MySQL包就OK了
- AOP的异常通知
一.配置异常通知的步骤 (Aspectj方式) 1.只有当切点报异常才能触发异常通知 2.在spring中有Aspectj 方式提供了异常通知方法 2.1 如果希望通过 schema-base 实 ...