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] ≠ ...
随机推荐
- Linux下ping加上时间戳
命令(ping10次): ping 192.168.5.160 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S", ...
- 第一个Python工程
创建你的第一个Python程序 如果你曾经很熟悉visual studio的工作方式.可能对python不习惯. 工程通常只与你使用的IDLE有关系.这些工具习惯将文档,编译,测试集成一体.所以就存在 ...
- java笔试面试题准备
J2SE基础 九种基本数据类型的大小,以及它们的封装类 byte 8 Byte char 16 Character short 16 Short int 32 Integer long 64 Long ...
- cf835(预处理 + 记忆化dp)
题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...
- 洛谷P1275 魔板
P1275 魔板 题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状 ...
- jmeter-逻辑控制器之 交替控制器(实现2个请求每次只执行其中一个)
交替控制器: 案例:两个请求每次只能执行其中一个,可使用交替控制器. 1.线程组->添加->逻辑控制器->交替控制器 2.在控制下添加两个http请求.运行的时候第一次循环执行第一个 ...
- JAVA String.format()的使用
常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...
- jmeter小问题解决方案合集
问题1.在http请求,post的body中输入中文,显示乱码,怎么解决? 在jmeter的bin目录下,找到这个文件jmeter.properties,把jsyntaxtextarea.font.f ...
- (反NIM)
题目大意是和普通的NIM游戏一样,但是却是取到最后一个是输的,天真的以为就是反过来,其实并不是这样的 结论 先手必胜的条件为 ①:所有堆的石子数均=1,且有偶数堆. ②:至少有一个堆的石子数>1 ...
- java——模拟新浪微博用户注册
1.创建用户类,重写HashCode()和equals()方法: import java.util.*; public class User{ private String name; private ...