Status: Accepted
Runtime: 9 ms

题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个元素时,该元素就是最大的了。当然你也可以找最大值,二分法复杂度O(logn)。我的想法是找临时最高点,从左到右,理想情况下,从num[0]之后会值逐渐增大,只要遇到一个比前一元素小的,就找到了。复杂度O(n),这个最大值可能是num[n-1]。

代码:

 class Solution {
public:
int findPeakElement(const vector<int> &num) {
if(num.size()==) return ;
if(num.size()==) return num[]>num[]?:;
int max=num[],ind=; for(int i=;i<num.size();i++)
{
if(num[i]>max)
{
max=num[i];
ind=i;
}
else break;
}
return ind;
}
};

Find Peak Element

附上二分法的代码,因为LeetCode是要在一个类中的一个函数实现,无法更好地利用递归二分法。以下代码是别处COPY的,因为在实现时发现每次递归都需要复制一次数组num,也就需要nlogn的盏空间了,数组大的话就不好了。不推荐此法,当然也可以用非递归二分法。
Status: Accepted
Runtime: 10 ms

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

Find Peak Element

LeetCode Find Peak Element 找临时最大值的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

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

  2. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  3. LeetCode: Find Peak Element 解题报告

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

  4. LeetCode Find Peak Element [TBD]

    说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...

  5. [LeetCode] Find Peak Element 二分搜索

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

  6. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  7. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  8. ✡ leetcode 162. Find Peak Element --------- java

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

  9. 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] ≠ ...

随机推荐

  1. 用ORBSLAM2运行TUM Dataset数据集Monocular Examples

    参照https://github.com/raulmur/ORB_SLAM2/blob/master/README.md 运行 4. Monocular Examples TUM Dataset 数据 ...

  2. 萌新在线模板--keyboarder_zsq

    好像马上就要出去打铁了QAQ,所以是不是要做个模板带过去也玩一玩? 那就做吧... 标题就设为萌新模板吧...各种萌新讲解对吧.... 图论 拓扑排序 最短路 最小生成树 二分匹配 强连通Tarjan ...

  3. 清橙 A1210. 光棱坦克

    A1210. 光棱坦克 时间限制:1.0s   内存限制:512.0MB   总提交次数:   AC次数:   平均分:   将本题分享到:        查看未格式化的试题   提交   试题讨论 ...

  4. Maven中常用插件的配置

    在Maven项目的pom.xml文件中配置插件信息,使用<build></build>标签 1.配置JDK版本插件和Tomcat版本插件 <build> <! ...

  5. 大话Spark(2)-Spark on Yarn运行模式

    Spark On Yarn 有两种运行模式: Yarn - Cluster Yarn - Client 他们的主要区别是: Cluster: Spark的Driver在App Master主进程内运行 ...

  6. 死磕 java同步系列之synchronized解析

    问题 (1)synchronized的特性? (2)synchronized的实现原理? (3)synchronized是否可重入? (4)synchronized是否是公平锁? (5)synchro ...

  7. Uva12174

    #include <bits/stdc++.h> using namespace std; ; int t; int s,n; ]; ]; ]; void init(){ memset(a ...

  8. java日期与时间戳相互转换大全

    转载大神 https://blog.csdn.net/djc777/article/details/50904989/

  9. 记录一个在线压缩和还原压缩js代码的工具

    packer – javascript 压缩工具 http://dean.edwards.name/packer/ Javascript Beautifier ---可以恢复某些压缩工具压缩的js代码 ...

  10. Solve Equations HackerRank 扩展欧几里德 && 数学

    https://www.hackerrank.com/contests/infinitum16-firsttimer/challenges/solve-equations 给定一条方程a*x + b* ...