LeetCode Find Peak Element 找临时最大值
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 找临时最大值的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- LeetCode Find Peak Element [TBD]
说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...
- [LeetCode] Find Peak Element 二分搜索
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- ✡ 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] ≠ ...
- 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] ≠ ...
随机推荐
- 洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...
- 多实例:MySQL系列之二
MySQL的多实例配置 在一台物理机中需要多个测试环境,那么就需要用到了搭建数据库的多个实例,多个实例的意思就是运行多份程序,实例与实例之间没有影响.要注意监听的端口需要不同. 环境:CentOS ...
- ie-"此更新不适应于此电脑"
cmd-dos命令 expand –F:* C:\update\Windows6.1-KB2533623-x64.msu C:\update\ dism.exe /online /Add-Packag ...
- POJ1024 Tester Program
题目来源:http://poj.org/problem?id=1024 题目大意: 有一个迷宫,迷宫的起点在(0,0)处.给定一条路径,和该迷宫墙的设置,要求验证该路径是否为唯一的最短路径,该种墙的设 ...
- Python之PIP安装
Python有两个著名的包管理工具easy_install.py和pip.Python2.7的安装包中自带了easy_install.py,而pip需要手动安装.而在Python3.5之后都是默认安装 ...
- 牛客练习赛43D(贪心)
有生之年我居然也能不看题解做出来题QAQ-- 发现c.d是0.1序列而不是随机数列说明有蹊跷,于是发现负数直接配0,正数配1即可.不知道哪个最小,那就全求一下吧--我的做法的坑点是数正好为1时不可以选 ...
- JAVA多线程之Volatiles
Java 语言中的 volatile 变量可以被看作是一种 “轻量级的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但 ...
- Windows 命令行基础(博主推荐)
不多说,直接上干货! 见 https://blog.henix.info/blog/windows-cmdbasic/
- Spark编程环境搭建及WordCount实例
基于Intellij IDEA搭建Spark开发环境搭建 基于Intellij IDEA搭建Spark开发环境搭——参考文档 ● 参考文档http://spark.apache.org/docs/la ...
- maven安装,使用说明,及maven Repository如何使用.
maven的使用方法总结一下 1.首先去apache官网下载maven, http://maven.apache.org/download.cgi2.如果是windows系统,选择 apache-ma ...