【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- ...
随机推荐
- Mysql或者Hive数据行变成列
对于mysql / hive 再进行统计的时候假设须要行变成列,能够使用函数 CASE 字段a WHEN 值b THEN c [WHEN d THEN e]* [ELSE f] END 当字段a=值 ...
- uboot之run_command简单分析
本文档简单分析了uboot中命令的实现.run_command函数的实现以及从uboot命令行接收并处理命令的过程. 作者: 彭东林 邮箱: pengdonglin137@163.com http:/ ...
- golang中并发sync和channel
golang中实现并发非常简单,只需在需要并发的函数前面添加关键字"go",但是如何处理go并发机制中不同goroutine之间的同步与通信,golang 中提供了sync包和channel ...
- 调整图像的尺寸 - cvResize() 函数实现
前言 有时会碰到一张图片太大了,想将它缩小.本文将讲解一个很好用的函数解决这个问题. 图像尺寸调整函数 cvResize() // 图像尺寸调整函数 void Resize ( const CvArr ...
- Android TextView setText卡顿问题
TextView 是经常使用控件之中的一个,最经常使用的方法是setText() . 可是 我们在显示大量的文本的时候,使用setText还是会有一些性能的问题. 这篇文章 关于TextView的s ...
- IOS开发之----异常处理
本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_ ...
- 再看GS接包过程
再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...
- 九度OJ 1156:谁是你的潜在朋友 (并查集)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5802 解决:2593 题目描述: "臭味相投"--这是我们描述朋友时喜欢用的词汇.两个人是朋友通常意味着他们存在着许多 ...
- mysql系列之3.mysql进阶
启动原理 mysqld脚本-->mysqld_safe脚本-->mysqld服务-->启动mysql 强制关闭mysql: 三种方法, 不建议用! killall mysqld pk ...
- mooc课程mit 6.00.1x--problem set3解决方法
RADIATION EXPOSURE 挺简单的一道题,计算函数和算法过程都已经给出,做一个迭代计算就行了. def radiationExposure(start, stop, step): ''' ...