LeetCode 852. Peak Index in a Mountain Array
LeetCode 852. Peak Index in a Mountain Array
Solution1:我的答案
时间复杂度O(n)O(n)O(n)
class Solution {
public:int peakIndexInMountainArray(vector<int>& A) {for (int i = 1; i < A.size() - 1; i++) if (A[i] > A[i-1] && A[i] > A[i+1])return i;}
};
Solution2:
参考网址:https://leetcode.com/problems/peak-index-in-a-mountain-array/discuss/139891/c++-O(log-n)binary-serach
二分查找,时间复杂度O(logn)O(logn)O(logn)
class Solution {
public:int peakIndexInMountainArray(vector<int>& A) {int l = 0,u= A.size()-1;int mid = (l+u)/2;while( !(A[mid]>A[mid-1] && A[mid]>A[mid+1]) ){if(A[mid]>A[mid-1]) l = mid+1;else u=mid-1;mid = (l+u)/2;}return mid;}
};
应该是要用二分查找做,否则也太无聊了
LeetCode 852. Peak Index in a Mountain Array相关推荐
- C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4003 访问. 我们把符合下列属性的数组 A 称作山脉: A.le ...
- (java)leetcode852 山脉数组的封顶索引(二分查找法找出数组中最大值的下标)(Peak Index in a Mountain Array)
题目描述: 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... ...
- 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...
- [Golang]力扣Leetcode - 852. 山脉数组的峰顶索引(二分查找)
[Golang]力扣Leetcode - 852. 山脉数组的峰顶索引(二分查找) 题目:符合下列属性的数组 arr 称为 山脉数组 : arr.length >= 3 存在 i(0 < ...
- LeetCode 961 N-Repeated Element in Size 2N Array --python,java解法
题目地址:N-Repeated Element in Size 2N Array - LeetCode Acceptance:73.3% Difficulty:Easy In a array A of ...
- LeetCode 442. Find All Duplicates in an Array
442. Find All Duplicates in an Array Add to List Description Submission Solutions Total Accepted: 16 ...
- leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- leetcode 453,462. Minimum Moves to Equal Array Elements I, II | 453, 462. 最少移动次数使数组元素相等(图解)
453. Minimum Moves to Equal Array Elements https://leetcode.com/problems/minimum-moves-to-equal-arra ...
最新文章
- windows10 中 安装 ffmpeg
- java虚拟机参数-X 与 -XX的区别
- logging下的path配置的是自己本地项目下的地址
- MySQL对in里面数据进行指定排序:order by field
- 超声射频信号的产生(RF Signal)
- Spring Aop总结
- (三)微调VGG16以对服装进行分类
- Android 简单实现控件的拖动
- 2019下半年计算机软考报名官网,2019下半年软考报名流程、软考报名方法
- 数学分析教程(科大)——6.3笔记+习题
- Java1.8开始为什么用元空间替换永久代
- 七种方式实现三栏布局
- 关于openssl 的几个key的生成方式
- TIA protal与SCL从入门到精通(5)——函数终止跳转处理
- 小鸟云服务器怎么修改密码?
- 商法——企业法律风险防范
- #420 Div2 Problem B Okabe and Banana Trees (math 暴力枚举)
- 算法训练:嘘,别人我不告诉TA
- ubuntu的一些安装设置
- 深入讲解VsCode各场景高级调试与使用技巧
热门文章
- 重点推荐:HP大中华区总裁孙振耀退休感言
- *args和**kargs
- yolov3前向传播(一)-- darknet53网络解析与实现
- mysql阻塞 事务_MySQL Innodb如何找出阻塞事务源头SQL
- qt 读取gif一帧_译:Unreal渲染一帧详解(Unreal Frame Breakdown)
- 555定时器回差电压计算公式_555定时器及其应用
- python线程池原理_Django异步任务线程池实现原理
- php imagefill,PHP图像处理技术实例总结【绘图、水印、验证码、图像压缩】
- 三菱q系列特殊继电器一览表_2020山西三菱Q系列PLC模块回收购销
- layui select第一次无法选中_Windows 10 无法访问共享的解决办法大全