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. Codeforces Round #302 (Div. 2) C. Writing Code 简单dp

    C. Writing Code Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...

  2. 如何判断c语言的变量类型

    变量三要素: 一个变量有三个基本的要素,变量的名称,变量的类型,变量的值.所以int a = 10; 变量名为a,变量的存储类型为int型,变量的值为10. 变量还有一些属性如作用范围和存储类型. 变 ...

  3. Use an LM317 as 0 to 3V adjustable regulator

    Most engineers know that they can use an inexpensive, three-terminal adjustable regulator, such as F ...

  4. [Projet] Module NFC

    http://www.f4grx.net/projet-module-nfc/ The NFC is a contactless communication technology, which is ...

  5. Java 反照机制中 getMethod()和getDeclaredField()区别

    Java 反射机制中 getMethod()和getDeclaredField()区别 今天在程序中用到java反射机制时,遇到的问题记录一下:我当时遇到的问题是,我用反射getMethod()调用类 ...

  6. mysql中函数greatest 与MAX区别

    greatest (a,b,c,d,d)max(a) 这样就能看明白了,greatest 求的是某几列的最大值,横向求最大(一行记录)max(a) 一看就明白了,是给纵向求最大(多行记录).

  7. easyui icon的使用相关

    easyui的默认图标有以下这些: .icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background: ...

  8. poj 1330 Nearest Common Ancestors 题解

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24618   Accept ...

  9. 如何更换Office 2013的product key?

    第一步 第二步 第三步 ... ... ... ... ^_^   参考资料 ======================== Change Product Key Office 2013 Home ...

  10. Echarts使用dataset数据集管理数据

    1.可以看官网api的入门例子 使用常见的对象数组的格式 option = { legend: {}, tooltip: {}, dataset: { // 这里指定了维度名的顺序,从而可以利用默认的 ...