【LeetCode】162. Find Peak Element (3 solutions)
Find Peak Element
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
Your solution should be in logarithmic complexity.
这题就是求序列最大值。顺序查找或二分查找均可。
满足复杂度要求的话需要用二分查找。
解法一:顺序查找
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
if(n == )
return ;
if(nums[] > nums[])
return ;
if(nums[n-] > nums[n-])
return n-;
for(int i = ; i < n-; i ++)
if(nums[i] > nums[i-] && nums[i] > nums[i+])
return i;
}
};

解法二:二分查找(递归)
class Solution {
public:
int findPeakElement(vector<int>& nums) {
return Helper(nums, , nums.size()-);
}
int Helper(vector<int>& nums, int low, int high)
{
if(low == high)
return low;
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
return Helper(nums, low, mid);
else
return Helper(nums, mid+, high);
}
};

解法三:二分查找(迭代)
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int low = ;
int high = nums.size()-;
while(low < high)
{
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
high = mid;
else
low = mid+;
}
return low;
}
};

【LeetCode】162. Find Peak Element (3 solutions)的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【刷题-LeetCode】162 Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 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] ≠ ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- 【Lintcode】075.Find Peak Element
题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
随机推荐
- hihocoder编程收割赛20
hihocoder编程收割赛20 hihocoder1542 : 无根数变有根树 hihocoder1542 思路: 树的遍历 ac代码: // hihocompete20_01.cpp : 定义控制 ...
- Windows蓝屏dump文件查看器(转)
Windbg-分析Windows蓝屏原因利器[转]下载地址先声明下,虽然用windbg诊断蓝屏之前网络上已经有人发过教程了,但就我而言, 学会使用windbg来诊断蓝屏也算是自己的原创吧.以前看一个微 ...
- iOS学习之sqlite的创建数据库,表,插入查看数据
目录(?)[-] 新建项目sqliteDemo添加使用sqlite的库libsqlite3dylib sqlite 的方法 获取沙盒目录并创建或打开数据库 创建数据表 插入数据 查询数据库并打印数据 ...
- introduction to my business card
http://www.t4f.org/projects/business-card/ After 4 years working in an international IT consulting c ...
- cocos2d3.0 Scale9Sprite
使用Scale9Sprite须要引入以下的头文件 #include "extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; 一 ...
- SpringMVC杂记(1) 使用阿里巴巴的fastjson
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- bootstrap设计站点中加入�代码高亮插件
这款插件的名字叫做google-code-prettify 使用该插件之前的效果: 使用插件之后的效果: 接下来说步骤: (1)下载两个文件 http://codecloud.sinaapp.com/ ...
- [Android 新特性] Android 4.3新功能(正式发布前)
腾讯数码讯(编译:徐萧梓丞)虽然谷歌公司目前尚未正式对外发布最新的Android 4.3果冻豆操作系统,但是在上周我们已经看到了关于三星正 在为原生版Galaxy S4进行Android 4.3系统进 ...
- mac活动监视器 的含义
应用内存:应用所使用的内存数量. 联动内存:系统运行需要的内存.联动内存不能缓存且必须存放在内存中,所以不能被其他应用使用. 压缩:为腾出更多内存而压缩的内存数量.当电脑接近其最大内存能力时,内存中的 ...
- iOS:CoreData数据库的使用二(创建多个数据库表,表之间有对应关系)
CoreData数据库框架是一个封装性好,功能强大数据库,它底层使用的还是sqlite数据库,不过苹果公司在其基础上,为其封装新和安全性的维护上做了大量的处理,例如对一些事物做了详细的操作,如读脏数据 ...