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. leetcode187. Repeated DNA Sequences

    https://leetcode.com/problems/repeated-dna-sequences/#/description   https://leetcode.com/problems/r ...

  2. centos安装gcc

    1.安装gcc基本开发工具环境 yum groupinstall 'Development Tools' 2.安装完成后查看安装是否成功 whereis gcc 3.查看gcc版本 gcc --ver ...

  3. 客户端获取ip

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. SSM+Maven(教程二):Idea快速入门SSM+Maven框架。

    快速入门须知 这篇文章,直接应用已经搭建好的SSM框架.一般在公司里面,考虑框架的搭建.封装等问题,都由研发经理或者架构师完成,所以对于刚入门的小白来说,在去搭建整合花费的时间会很多很多.对于理解能力 ...

  5. Struts2 include(包含)多个配置文件

    Struts 2自带有“包含文件”功能,包含多个Struts配置文件合并为一个单元. 单个Struts配置文件 让我们来看看一个糟糕的 Struts 2 配置示例. struts.xml <?x ...

  6. 角摩网发布在线制作Epub、Mobi格式的电子书

    原来cn的域名没有及时续约被人用了,现在用www.joymo.cc开始新的电子书制作之路. 目前支持Epub和Mobi格式,会陆续加入PDF和APK的电子书.

  7. Git使用教程(转载)

    Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是 ...

  8. 使用swig将C/C++代码转为JAVA接口(Windows平台)

    小弟一直没用过Linux. 平时的码也只是在WINDOW上用SWIG或CYGWIN进行编译. 下面的例子,先从网上找来一个.c文件. example.c /* File : example.c */ ...

  9. WEB漏洞挖掘技术总结

    漏洞挖掘技术一直是网络攻击者最感兴趣的问题,漏洞挖掘的范围也在随着技术的提升而有所变化.在前期针对缓冲区溢出.格式化字符串.堆溢出.lib库溢出等技术都是针对ELF文件(Linux可执行文件)或者PE ...

  10. Fragment 创建 传递参数 跳转【典例】

    Fragment一定要有一个无参的构造方法! 因为当Activity因屏幕旋转或者因内存不足被系统杀死时,会导致Activity被重新创建,而当Activity被重建时,FragmentManager ...