【Lintcode】075.Find Peak Element
题目:
There is an integer array which has the following features:
- The numbers in adjacent positions are different.
- A[0] < A[1] && A[A.length - 2] > A[A.length - 1].
We define a position P is a peek if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.
Example
Given [1, 2, 1, 3, 4, 5, 7, 6]
Return index 1
(which is number 2) or 6
(which is number 7)
题解:
class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
if (A.size() < ) {
return -;
} int n = A.size();
int start = ;
int end = n - ;
while (start < end - ) {
int mid = start + (end - start) / ;
if (A[mid] > A[mid - ] && A[mid] > A[mid + ]) {
return mid;
} else if (A[mid] > A[mid-]) {
start = mid;
} else {
end = mid;
}
} if (A[start] > A[start + ]) {
return start;
} else if (A[end] > A[end - ]) {
return end;
} return -;
}
};
【Lintcode】075.Find Peak Element的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- 【LeetCode】162. Find Peak Element (3 solutions)
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
[转]The content of element type "configuration" must match "(properties?,settings?,typ ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【刷题-LeetCode】162 Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
随机推荐
- eclipse--windowBuilder
https://www.eclipse.org/windowbuilder/ https://www.eclipse.org/windowbuilder/download.php Documentat ...
- debian jessie install note
Debian支持非常多的硬件,包括arm/mips/ppc/x86,于是想安装个Debian看看,也不想总屈服在canonical的ubuntu下面. 光盘镜像太多了 纯社区版的安装总是没有想像得那么 ...
- 深入Asyncio(九)异步生成器
Async Generators:yield inside async def functions 如果在async def中使用yield会发生什么,答案就是生成一个异步生成器函数,如果有生成器.协 ...
- 图像处理之基础---二维卷积c实现
http://wenku.baidu.com/link?url=4RzdmvP9sdaaUbnVEW4OyBD-g67wIOiJjKFF3Le_bu7hIiBS7I6hMcDmCXrQwsHvrsPv ...
- ubuntu 中文显示乱码问题 (转)
添加中文字符编码: $sudo vim /var/lib/locales/supported.d/local #添加下面的中文字符集 zh_CN.GBK GBK zh_CN.GB2312 GB2312 ...
- uva--10714+找规律
题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...
- linux 安装mongo
在Linux中安装Mongodb操作说明 MongoDB配置 版本说明:因本机所装Red Hat 为 64位操作系统故本例以64位的MongDB为例.所用版本如下: (1) Red Hat En ...
- Objective-C/C++混编编译器设置
本文转载至 http://blog.csdn.net/yu0089/article/details/8220275 当使用Xcode编写C++代码时,难以避免要使用xcode/C++混合编译. 如果你 ...
- python 基础 7.0 import 导入
一. python 常用内置模块的使用(datetime,logging,os,command) 在日常的开发工作中,我们要写很多的python 代码,如果都写在一个文件中,会导致代码特别 ...
- .Net反射机制
现在谈.Net反射机制本不在计划中,因为本打算研究完设计模式后再去学习别的技术:但迫于设计模式系列一创建型之(抽象工厂模式)一章中遗留问题,才不得已在设计模式之游中插入本篇文章!签于本人对反射理解也不 ...