Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

这题就是求序列最大值。顺序查找或二分查找均可。

满足复杂度要求的话需要用二分查找。

解法一:顺序查找

class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
if(n == )
return ;
if(nums[] > nums[])
return ;
if(nums[n-] > nums[n-])
return n-;
for(int i = ; i < n-; i ++)
if(nums[i] > nums[i-] && nums[i] > nums[i+])
return i;
}
};

解法二:二分查找(递归)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
return Helper(nums, , nums.size()-);
}
int Helper(vector<int>& nums, int low, int high)
{
if(low == high)
return low;
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
return Helper(nums, low, mid);
else
return Helper(nums, mid+, high);
}
};

解法三:二分查找(迭代)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
int low = ;
int high = nums.size()-;
while(low < high)
{
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
high = mid;
else
low = mid+;
}
return low;
}
};

【LeetCode】162. Find Peak Element (3 solutions)的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode OJ 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

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

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

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

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

  8. 【Lintcode】075.Find Peak Element

    题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...

  9. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

随机推荐

  1. 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交

    E - 秋实大哥与家 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  2. mssql Row_Number() 分页 DISTINCT 问题

    转载原文地址http://www.cnblogs.com/pumaboyd/archive/2008/04/20/1162376.html 这周碰到了很多奇怪的问题,有些是莫名的低级错误,有些这是一直 ...

  3. 掌握11项技能,你就是优秀的前端开发project师

    导读: 你或许会认为前端开发是一个非常easy的工作,对呀,你就是刚刚从网页设计转型过来的.但当你深入当中时,一定会发现好像前端开发不是那么简单,光站点性能优化.响应式.框架就让你焦头烂额, 确实,做 ...

  4. Eclipse 中导入jar包

    导入到工程即可使用了1. 在程序目录下,创建一个文件夹,如 lib. 2. 将第三方jar包复制到该目录下3.右键工程,选择Build path4.java build path,选择 librari ...

  5. 【mysql】二级索引----聚簇索引和非聚簇索引-----

    参考地址: https://blog.csdn.net/bigtree_3721/article/details/51335479 https://blog.csdn.net/roxliu/artic ...

  6. FaceBook推出的Android图片加载库-Fresco

    FaceBook推出的Android图片加载库-Fresco 原文链接:Introducing Fresco: A new image library for Android 译者 : ZhaoKai ...

  7. IIS7 win7 x64 MVC部署

    .net4.5已经装好,mvc4setup也装了,启动IIS后打开网页还是不能正常显示页面,404错误 最后发现把win7升级到SP1就正常了,具体是那个补丁修复的就不知道了

  8. Delphi 6 保存窗体设置

    DSK Desktop Setting File 保存工程文件的桌面摆布情况, 下次打开时可以恢复上次保存的桌面状态 Desktop文件.保存了IDE的布局(也可能包含浏览记号,视乎IDE的设定),为 ...

  9. sqlalchemy: TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

    mysql建立的连接要及时删除,不然连接池资源耗尽 相关文章参考: http://blog.csdn.net/robinson1988/article/details/4713294 http://b ...

  10. ECMAScript新特性【一】--Object.create

    Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数: prototype 必需.  要用作原型的对象. 可以为 nu ...