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 ...
随机推荐
- CentOS6.5更改语言设置
yum grouplist |grep cn yum groupinstall “Chinese Support”——————————————yum groupinstall “Desktop”vi ...
- POJ3349 Snowflake Snow Snowflakes (JAVA)
首先声明代码并没有AC,内存超了 但我对此无能为力,有没有哪位大神好心教一下怎么写 哈希,然后比较花瓣数组,这些应该都没问题才对..唉.. 贴MLE代码 import java.util.*; pub ...
- HTTP请求处理流程-SpringMvc
1.在SpringMVC的http请求处理过程中,包括了前端控制器(DispatcherServlet).处理映射器(HandlerMapping).处理适配器(HandlerAdapter).处理器 ...
- Vue项目中用的Jquery.js和easing.js做的抛物线
如果项目中还用到自有的$ <script> jQuery.noConflict();</script> //星星掉落movePathNew(args) { let self = ...
- Comparación para 2019 Nueva Lonsdor K518S y K518ISE
Comparación para 2019 Nueva Lonsdor K518S y Lonsdor K518ISE: Igual: Capacidades de Immo y cobertura ...
- zabbix4.2 安装
官网https://www.zabbix.com/cn/download a. 安装 数据库rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86 ...
- 文献综述十四:基于Oracle11g的超市进销存管理系统设计与实现
一.基本信息 标题:基于Oracle11g的超市进销存管理系统设计与实现 时间:2016 出版源:技术创新 文件分类:对数据库的研究 二.研究背景 为超市设计开发的超市管理系统,采用的是 VC+ Or ...
- Python多线程&进程
一.线程&进程 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程, ...
- ios UITableView 搜索
自己实现 UITableView 搜索,相对于使用 UISearchDisplayController 来说自己写稍微麻烦了那么一点点,但是更加灵活.主要就是用一个字段区分出当前是搜索还是非搜索,然后 ...
- Apache 配置虚拟域名的最简单方式
一.配置httpd.conf: 1.取消Include conf/extra/httpd-vhosts.conf的注释,代码如下: # Virtual hostsInclude conf/extra/ ...