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 ...
随机推荐
- [Swift]字符串大小写转换,同时实现本地化或设置语言环境
在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...
- django2使用xadmin打造适合国人的后台管理系统(1)
python火了之后,学习python的人也越来越多了,python做web开发的话,flask.django是比较火的框架了,django是一个比较大的框架,也是一个快速开发利器.但是,django ...
- MODBUS协议相关代码(CRC验证 客户端程序)
Modbus协议是一种已广泛应用于当今工业控制领域的通用通讯协议.通过此协议,控制器相互之间.或控制器经由网络(如以太网)可以和其它设备之间进行通信.Modbus协议使用的是主从通讯技术,即由主设备主 ...
- [CoffeeScript]在WebStorm里运行CoffeeScript
CoffeeScript 是一门编译到 JavaScript 的小巧语言. 在 Java 般笨拙的外表下, JavaScript 其实有着一颗华丽的心脏. CoffeeScript 尝试用简洁的方式展 ...
- 高阶篇:4.1)QFD质量功能展开-总章
本章目的:了解QFD概念和作用,为FMEA打下基础. 1.QFD定义 质量功能展开QFD(Quality Function Deployment),是把顾客或市场的要求转化为设计要求.零部件特性.工艺 ...
- ZOJ - 2112 主席树套树状数组
题意:动态第k大,可单点更新,操作+原数组范围6e4 年轻人的第一道纯手工树套树 静态第k大可以很轻易的用权值主席树作差而得 而动态第k大由于修改第i个数会影响[i...n]棵树,因此我们不能在原主席 ...
- 为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比
原文:https://www.sojson.com/blog/48.html 前言: MQ 是什么?队列是什么,MQ 我们可以理解为消息队列,队列我们可以理解为管道.以管道的方式做消息传递. 场景: ...
- ifconfig无输出的原因及解决办法
问题 执行 ifconfig 命令无任何报错,也无任何输出信息 [root@linuxprobe ~]# ifconfig[root@linuxprobe ~]# 排错 1. 检查PATH变量 [r ...
- request.upload.addEventListener in not a function。
本人在使用vue开发一套后台系统时,碰到了一个上传文件的需求,因为平时做的上传是使用ajax的方式来进行上传. 现在是使用axios来进行上传,方式没有什么改变: npm i axios 直接上 ...
- es6精华
函数: ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面.function log(x, y = 'World') { console.log(x, y); } log('Hello') ...