题目:

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的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  3. 【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 ...

  4. 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...

    [转]The content of element type "configuration" must match "(properties?,settings?,typ ...

  5. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  7. 【刷题-LeetCode】162 Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  8. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

  9. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

随机推荐

  1. 基于togglepoolmember.pl编写F5设备控制模块

    为了方便利用python对F5设备进行操作,本文将togglepoolmember.pl对F5设备的控制写成了python模块,源代码例如以下: #!/usr/bin/python # -*- cod ...

  2. android 5.0 默认水波纹背景属性,可设置不论什么View

    actionBarItemBackground   5.0以上超出边界圆形水波纹 selectableItemBackground  5.0以上边界内圆形水波纹 这两个属性在5.0下面是默认的灰色效果 ...

  3. 使用mysqld_multi 实现Mysql 5.6.36 + 5.7.18 单机多实例多版本安装

    Mysql 5.6.36 + 5.7.18 单机多实例多版本安装 随着硬件层面的发展,各种高性能服务器如雨后春笋般出现,但高性能服务器不免造成浪费, MySQL单机多实例,是指在一台物理服务器上运行多 ...

  4. 调整图像的尺寸 - cvResize() 函数实现

    前言 有时会碰到一张图片太大了,想将它缩小.本文将讲解一个很好用的函数解决这个问题. 图像尺寸调整函数 cvResize() // 图像尺寸调整函数 void Resize ( const CvArr ...

  5. Python学习总结之三 -- 优雅的字符串

    优雅的字符串 前言 记得我在Python学习总结第一篇中有提到字符串,那个可以算是先打个招呼吧,因为没有提到任何关于字符串的处理方法.今天,给大家详细讲解一下Python中字符串的使用方法,如有不当或 ...

  6. C#判断WebService接口是否可用

    using MSXML2; public bool InterfaceEnble() { string url = "http://localhost:81/WebServiceLogin. ...

  7. python中的特殊用法

    1 别名 from xxx import xxx as xxx;

  8. windows下安装mysql 开机启动

    1 下载地址 http://dev.mysql.com/downloads/installer/ 2 下载版本 mysql community server 5.7.x 这个版本是一个傻瓜版本,设置r ...

  9. mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'

    mongo 数据库提前关闭 // mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongod ...

  10. 题解 P1095 【守望者的逃离】

    贪心.数组都不用开那种. 考虑跑步距离的构成.发现跑步只有三种情况构成 休息 传送 朴素地跑 显然,如果可以传送,我们就不要朴素地跑步.因为\(17\le 60 \div 2 =30\). 假如我们知 ...