LeetCode 162 Find Peak Element
Problem:
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.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
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.
Summary:
找到数组中的局部最大数。
Solution:
1. 顺序查找:最直接的方法,复杂度为O(n)
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} for (int i = ; i < len; i++) {
if (!i && nums[i] > nums[i + ] ||
i == len - && nums[i] > nums[i - ] ||
nums[i] > nums[i - ] && nums[i] > nums[i + ]) {
return i;
}
} return -;
}
};
2. 二分查找:首先找到整体的中间值m,若m符合局部最大条件则返回m,否则若nums[m - 1] > nums[m]则在[0, m - 1]中查找。因为数组左边和右边为负无穷,所以在这种情况下[0, m - 1]中一定存在一个局部最大值。这种方法复杂度为O(logn)。
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} int l = , r = len - ;
while (l <= r) {
int m = (l + r) / ;
if ((!m || nums[m] >= nums[m - ]) &&
(m == len - || nums[m] >= nums[m + ])) {
return m;
}
if (m && nums[m] < nums[m - ]) {
r = m - ;
}
else {
l = m + ;
}
} return -;
}
};
二分查找的简略写法:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} int l = , r = len - ;
while (l < r) {
int m = (l + r) / ;
if (nums[m] > nums[m + ]) {
r = m;
}
else {
l = m + ;
}
} return r;
}
};
LeetCode 162 Find Peak Element的更多相关文章
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ✡ 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 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- ACM提交结果简介
如果你看到红色的"Accepted",那么,恭喜你,你已经成功的解决了该问题! 如果你收到的是如下的信息,则还需要继续检查你的程序: Wrong Answer (WA) : 输出结 ...
- 生成GUID唯一值的方法汇总(dotnet/javascript/sqlserver)
一.在 .NET 中生成1.直接用.NET Framework 提供的 Guid() 函数,此种方法使用非常广泛.GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的任何两台计 ...
- 《Javascript高级程序设计》读书笔记(1-3章)
第一章 JavaScript简介 1.1 JavaScript简史 略 1.2 JavaScript实现 虽然 JavaScript 和 ECMAScript 通常都被人们用来表达相同的含义,但 Ja ...
- JavaScript工具代码
html编码 function htmlEscape(sHtml){ return sHtml && sHtml.replace(/[<>&"]/g, f ...
- BZOJ1013: [JSOI2008]球形空间产生器sphere
传送门 高斯消元练习. 模板: void Guass(){ int waited; up(i,1,N){ waited=i; up(j,i+1,N)if(fabs(M[j][i])>fabs(M ...
- Windows bat脚本学习(1)
基础 首先所有命令在cmd命令行中都能找到说明: 例如 想知道type用法 输入type /? 其他命令都一样 type [drive:][path] filename 显示文本文件内容 虽然有点鸡肋 ...
- MSSQLServer中组织或分类表的设计及其递归查询
开篇:项目中用到上下级从属关系的太多太多了,如:组织.分类.行政区域,这里不再一一介绍,遇到这种的如何去进行数据库表的设计及其应用的,个人对往期项目中所涉及到的进行了一些总结. 数据库表设计:表字段一 ...
- thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印
今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...
- Make 命令教程
http://www.ruanyifeng.com/blog/2015/02/make.html 作者: 阮一峰 日期: 2015年2月20日 代码变成可执行文件,叫做编译(compile):先编译这 ...
- Win7硬盘整数分区一览表
10G=10245 MB 20G=20482 MB 30G=30726 MB 40G=40963 MB 50G=51208 MB 60G=61444 MB 70G=71681 MB 80G=81926 ...