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.

Notice

The array may contains multiple peeks, find any of them.

Have you met this question in a real interview?

 
 
Example

Given [1, 2, 1, 3, 4, 5, 7, 6]

Return index 1 (which is number 2) or 6 (which is number 7)

Challenge

Time complexity O(logN)

LeetCode上的原题,请参见我之前的博客Find Peak Element

解法一:

class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
int left = , right = A.size() - ;
while (left < right) {
int mid = left + (right - left) / ;
if (A[mid] < A[mid + ]) left = mid + ;
else right = mid;
}
return right;
}
};

解法二:

class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
for (int i = ; i < A.size(); ++i) {
if (A[i] < A[i - ]) return i - ;
}
return A.size() - ;
}
};

最新文章

  1. 使用CocoaPods过程中 Unable to find a specification for
  2. Struts2_ValueStack,OGNL详解(转)
  3. centos BIND服务基础及域主服务器配置
  4. Hession矩阵与牛顿迭代法
  5. android 为TextView添加边框
  6. ajax提交到后台是中文乱码
  7. The area 积分积分
  8. python基础1之 由来、种类、优缺点、安装环境
  9. 1个汉字在UTF-8编码占3个字节
  10. JMeter&#160;逻辑控制之While循环控制器(While&#160;Controller)
  11. 软件常用设置(VC, eclipse ,nodejs)---自己备用
  12. python django简单的登陆实现
  13. scikit-FEM-例1-求解Possion边值问题
  14. mkubimage-mlc2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: No such file or directory
  15. elasticSearch6源码分析(1)启动过程
  16. find-k-pairs-with-smallest-sums
  17. Esper简介
  18. python可变容器类型做函数参数的坑
  19. 7. I/O复用
  20. c++输出保留固定小数位数

热门文章

  1. 重视blog备份——兼记我与CSDN的爱恨情仇
  2. Content-disposition
  3. Mosquitto搭建Android推送服务(二)Mosquitto简介及搭建
  4. VC++ 设置软件开机自启动的方法
  5. Erlang--etc结构解析
  6. mvc架构
  7. 登陆Oracle,报oracle initializationg or shutdown in progress 错误提示
  8. iOS 通信常用小功能
  9. for_each(c++11)
  10. C#调用win32 api 操作其它窗口