LeetCode Find Peak Element [TBD]
说要写成对数时间复杂度,算了想不出来,写个O(n)的水了
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int len = num.size();
if (len < ) {
return -;
}
if (len == ) {
return ;
}
bool asc = true;
int idx = ;
int last = num[idx++];
while (idx < len) {
int cur = num[idx];
if (asc) {
if (cur < last) {
return idx - ;
}
} else {
if (cur > last) {
asc = true;
}
}
last = cur;
idx++;
}
if (asc) {
return idx - ;
}
return -;
}
};
第二轮:
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.
O(n)的
// 9:43
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
}
if (nums[] > nums[]) {
return ;
} for (int i=; i<len-; i++) {
if (nums[i] > nums[i-] && nums[i] > nums[i+]) {
return i;
}
}
return len-;
}
};
从discuss(https://leetcode.com/discuss/23840/java-binary-search-solution)里找到一个logn的但是不是很明白:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
int lo = , hi = len - ;
while (lo < hi) {
int mid = (lo + hi) / ;
if (nums[mid] > nums[mid + ]) {
hi = mid;
} else {
lo = mid + ;
}
}
return lo;
}
};
由于规定了边界元素特征,在二分搜索的时候,都使得每个子空间尝试满足这个条件
LeetCode Find Peak Element [TBD]的更多相关文章
- [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 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- 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 二分搜索
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 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 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 ...
随机推荐
- 恢复 MSSQL bak 文件扩展名数据(下)
恢复 MSSQL bak 文件扩展名数据 一.概念: RESTORE Statements (Transact-SQL) Restores backups taken using the BACKUP ...
- Flink学习笔记:Flink API 通用基本概念
本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...
- C#-函数的传值与传址
传值就是将实参的值传到所调用的函数里面,实参的值并没有发生变化,默认传值的有int型,浮点型,bool型,char字符型,结构体等等. 传址就是将地址传到所调用的函数里面操作,实参的值也会跟着变化,传 ...
- Oracle批量插入数据SQL语句太长出错:无效的主机/绑定变量名
Oracle数据库,用mybatic批量插入数据: <insert id="saveBatch" parameterType="io.renren.entity.N ...
- 本地DataGrip连接阿里云MySQL
1.阿里云上开通MySQL端口 2.MySQL上的设置 1⃣️mysql -uroot -p2⃣️create user 'usrabc'@'%' identified by 'usrabc'; 3. ...
- checkbox 框 选中判断
function checkAll(checktop){ $(":checkbox[name='id']").prop("checked",checktop.c ...
- Oracle 维护数据的完整性 一 索引
简介:索引是用于加速数据存取的数据对象,合理的使用索引可以大大降低i/o 次数,从而提高数据的访问性能. 当我们从一张表中检索我们需要的数据是,oralce往往会进行全表扫描,就是遍历所有的数据行,来 ...
- 我的Python升级打怪之路【六】:面向对象(一)
面向对象的概述 面向过程:根据业务逻辑从上到下写代码 函数式:将其功能代码封装到函数中,日后便无需编写,仅仅调用即可 [执行函数] 面向对象:对函数进行分类和封装.[创建对象]==>[通过对象执 ...
- Robot Framework自动化测试一(第一个脚本)
创建测试项目 选择菜单栏file----->new Project Name 输入项目名称,Type 选择Di ...
- 针对浏览器不支持JavaScript的简单处理
简单的思路是这样的: 在网页中显示某些内容,作为不支持JS的提示, 然后在页面载人的时候执行一段JS代码,代码的功能就是隐藏那个提示不支持JS的代码 具体内容看例子: <html> < ...