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 ...
随机推荐
- 获取Android状态栏的高度
Android 开发中经常需要知道屏幕高度.宽度.状态栏,标题栏的高度等 宽度和高度 WindowManager windowManager = (WindowManager) getSystemSe ...
- 关于在iOS应用中跳转到AppStore
1.获取app 在AppStore上的网址 eg: NSString * appURLStr = @"https://itunes.apple.com/cn/app/shi-ke-zu-qi ...
- 达人篇:2.1)APQP产品质量先期策划
本章目的:介绍APQP的概念,明确APQP各个阶段提交的内容.理解APQP是帮助而不是负担. APQP概念: 产品质量先期策划(Advanced Product Quality Planning,简称 ...
- 评估指标:ROC,AUC,Precision、Recall、F1-score
一.ROC,AUC ROC(Receiver Operating Characteristic)曲线和AUC常被用来评价一个二值分类器(binary classifier)的优劣 . ROC曲线一般的 ...
- 51nod 1943 联通期望 题解【枚举】【二进制】【概率期望】【DP】
集合统计类期望题目. 题目描述 在一片大海上有 \(n\) 个岛屿,规划建设 \(m\) 座桥,第i座桥的成本为 \(z_i\),但由于海怪的存在,第 \(i\) 座桥有 \(p_i\) 的概率不能建 ...
- vue-cli 启动过项目步骤
一. 安装 node.js 安装完成后,可以命令行工具中输入 node -v 和 npm -v,如果能显示出版本号,就说明安装成功. 二.安装webpack npm install webpack - ...
- linux忘记root密码怎么办
如何找回root密码,如果我们不小心,忘记root密码,怎么找回? 思路:进入到单用户模式,然后修改root密码.因为进入单用户模式,root不需要密码就可以登录. 详细过程: 1.打开虚拟机 2.开 ...
- java 的继承
1 为什么要使用继承? 为了提取两个类中公共的代码,可以使用继承抽取重复性的代码到一个公共类中,这个公共的类称为父类(super class).继承于父类的类称为子类(sub class). java ...
- Java 数组算法列题解析
1.声明一个char类型的数组, 从键盘录入6个字符[1]遍历输出[2]排序[3]把char数组转化成一个逆序的数组 总结:分析问题时,先问自己,需要用到什么? 对于这题,第一步:先写一个main方 ...
- Oracle初始化数据库表空间、用户、表(索引、分区)等
[oracle@bogon orcl]$ sqlplus sys/sys as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Oct 8 ...