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] ≠ ...
随机推荐
- Java中Object类的公有方法
HashCode();wait();notify();equals();getClass();toString();clone();finalize(); 这里只是简单介绍一下其中的几个函数: Has ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)D(思维,DP,字符串)
#include<bits/stdc++.h>using namespace std;char c[2007][2007];char ans[4007];int s[2007][2007] ...
- Trie[字典树] 数据结构及基本操作集
#include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...
- Python-OpenCV中图像颜色空间转换
目录 cv2.cvtColor() 1. RGB to GRAY 2. RGB to CIE XYZ 3. RGB to YCrCb JPEG 4. RGB to HSV 5. RGB to HLS ...
- 水库(树形dp)
水库 (树形dp) R国有n座城市和n-1条长度为1的双向道路,每条双向道路连接两座城市,城市之间均相互连通.现在你需要维护R国的供水系统.你可以在一些城市修建水库,在第i个城市修建水库需要每年c_i ...
- 洛谷P2052 道路修建
P2052 道路修建 题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...
- Meter - 连续性能测试 - JMeter + ANT + Jenkins集成 - 第1部分
目标: 创建包含性能测试流程的持续交付管道, 以尽早检测任何与性能相关的问题. 通常,全面的性能测试将在分段/预生产环境中完成,该环境可能与您的生产环境相同.在完成QA功能/回归验证后,将代码推送到分 ...
- PartTime_网址_国外
https://www.douban.com/group/topic/6248314/ 国外威客网站大全 国外兼职网站,以及国外外包网站.这些国外项目网站包括的项目类型很多:logo设计.图形设计.f ...
- Python 魔术方法及调用方式
魔术方法 调用方式 解释 __new__(cls [,...]) instance = MyClass(arg1, arg2) __new__ 在创建实例的时候被调用 __init__(self [, ...
- Linux网络管理命令ifdown/ifup与ifconfig/ip中的down/up命令的对比
参考了:https://blog.csdn.net/GDUTLYP/article/details/50498202 以下网卡均采用eth1说明. 相同点——[启用]和[禁止]网卡 ifdown et ...