【Lintcode】寻找峰值
来源:互联网 发布:技术博客rss 知乎 编辑:程序博客网 时间:2024/05/18 00:20
描述
你给出一个整数数组(size为n),其具有以下特点:
相邻位置的数字是不同的
A[0] < A[1] 并且 A[n - 2] > A[n - 1]
假定P是峰值的位置则满足A[P] > A[P-1]且A[P] > A[P+1],返回数组中任意一个峰值的位置。
java code
class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { // write your code here int start = 0; int end = A.length - 1; int mid = 0; while (start + 1 < end) { mid = start + (end - start) / 2; if (A[mid - 1] < A[mid] && A[mid] > A[mid + 1]) { return mid; } if (A[mid - 1] < A[mid] && A[mid] < A[mid + 1]) { // 上升区间 start = mid; continue; } if (A[mid - 1] > A[mid] && A[mid] > A[mid + 1]) { // 下降区间 end = mid; continue; } start = mid; } if (A[start] < A[end]) { return end; } else { return start; } }}
阅读全文
0 0
- LintCode-寻找峰值
- [LintCode]寻找峰值
- lintcode:寻找峰值
- LintCode 75 寻找峰值
- LintCode : 寻找峰值
- Lintcode 寻找峰值
- LintCode 75 寻找峰值
- 寻找峰值-LintCode
- 【Lintcode】寻找峰值
- lintcode--寻找峰值
- Google/LintCode:M-寻找峰值
- [Lintcode]Find Peak Element 寻找峰值
- 寻找峰值 查看运行结果 ——LintCode
- 寻找峰值
- 寻找峰值
- 寻找峰值
- 寻找峰值
- 寻找峰值
- eclipse、maven插件安装
- leetcode 63. Unique Paths II
- nsis接收命令行参数,实现动态安装
- The entity type <type> is not part of the model for the current context
- 面试内容
- 【Lintcode】寻找峰值
- IP数据报格式详解
- ListView中headerview的动态显示和隐藏的问题
- 使用NavicatforMySQL导入sql文件,如果表中有中文字符就会报错
- git 常用命令
- 解决问题:安装ubuntu时出现 (initramfs)unable to find a medium containing a live file
- Spring Boot基础
- qsort函数应用大全(转)
- 总结梯度优化算法