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] ≠ ...
随机推荐
- [转]简短介绍 C# 6 的新特性
原文地址:http://www.oschina.net/translate/briefly-exploring-csharp-new-features 几周前我在不同的地方读到了有关C#6的一些新特性 ...
- SQL Server修改主、外键和约束
0.创建表 create table Users ( Id int, Name ), Phone ), Email ), Role_Id uniqueidentifier ) go create ta ...
- EasyUI 在mvc中的引入与使用
使用步骤: 一.先引入: 1.easyui下载,可以去官方网站去下载最新版EasyUI官方下载地址:http://www.jeasyui.com/download/index.php 2.下载后解压, ...
- oracle 导入sql文件乱码
查看 http://blog.csdn.net/fireofjava/article/details/53980966
- Fetch超时设置和终止请求
1.基本使用 Fetch 是一个新的端获取资源的接口,用于替换笨重繁琐XMLHttpRequest.它有了Request 和 Response 以及Headers对象的概念,与后端语言请求资源更接近. ...
- 安装XPS文件查看器的方法
方法1https://jingyan.baidu.com/article/ca2d939d6eb0eeeb6c31cecb.html 方法2 Win10需要使用这种方式,因为Windows 10:版本 ...
- Spark BlockManager 概述
Application 启动的时候: 1. 会在 SparkEnv 中实例化 BlockManagerMaster 和 MapOutputTracker,其中 (a) BlockManagerMast ...
- poj 2068 Nim
Nim POJ - 2068 题目大意:多组数据,两人轮流操作,n轮一循环,给出总石子数和这n轮每次两人能取的石子上限(下限为1).取到最后一颗者输. /* f[i][j]表示在第i轮中一共有j个石子 ...
- [Xcode 实际操作]三、视图控制器-(11)在Storyboard中使用表格控件
目录:[Swift]Xcode实际操作 本文将演示表格控件在故事板中的使用. 点击[显示或隐藏检查器按钮],再界面右侧打开检查器面板. 在控制器根视图上点击鼠标,以选择该根视图. 现在往根视图中添加一 ...
- github上虽然已经有实现好的Char RNN
前言 学习RNN的时候很多人应该都有看过Andrej Karpathy写的The Unreasonable Effectiveness of Recurrent Neural Networks,使用基 ...