LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy
方法一:二分查找
int peakIndexInMountainArray(vector<int>& A) {
// insert another two elements to avoid out of bound
const int INT_MAX_ = 2147483647;
const int INT_MIN_ = (-INT_MAX_-1);
// insert INT_MIN_ before A.begin()
A.insert(A.begin(),INT_MIN_);
A.push_back(INT_MIN_);
int left = 1, right = A.size()-2;
// binary search
while(left <= right) {
int mid = left + (right - left) /2;
if(A[mid-1] < A[mid] && A[mid] > A[mid+1]) return mid-1;
if(A[mid-1] < A[mid] && A[mid] < A[mid+1]) left = mid + 1;
if(A[mid-1] > A[mid] && A[mid] > A[mid+1]) right = mid - 1;
}
return -1;x
}
官方答案:
class Solution {
public int peakIndexInMountainArray(int[] A) {
int lo = 0, hi = A.length - 1;
while (lo < hi) {
int mi = lo + (hi - lo) / 2;
if (A[mi] < A[mi + 1])
lo = mi + 1;
else
hi = mi;
}
return lo;
}
}
- Time Complexity: O(logN)
- Space Complexity: O(1)
方法二:遍历找到数值下降的点
略。
参考:
LeetCode 852. Peak Index in a Mountain Array C++ 解题报告的更多相关文章
- LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
题目标签:Binary Search 题目给了我们一组 int array,让我们找到数组的 peak. 利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边 ...
- LeetCode 852 Peak Index in a Mountain Array 解题报告
题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- leetcode 852. Peak Index in a Mountain Array
Input: [0,1,0] Output: 1 Input: [0,2,1,0] Output: 1解: 比较数组中的i和i-1的大小,如果前一位大于后一位数字,前一位则是结果 let ans = ...
- 【Leetcode_easy】852. Peak Index in a Mountain Array
problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- 852. Peak Index in a Mountain Array
class Solution { public: int peakIndexInMountainArray(vector<int>& A) { return max_element ...
- Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)
Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...
- [LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
随机推荐
- 关于Servlet的一些归纳(2)
1.web项目结构 根路径: 文件夹 文件 WEB-INF: lib(存放一些jar文件) classes(存放class文件) web.xml 2.GenericServlet类 实现了Servle ...
- MapReduce实现Apriori算法
Apiroi算法在Hadoop MapReduce上的实现 输入格式: 一行为一个Bucket 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 ...
- LINUX安装vm tools及使用方法(centos7,vm12)
1.安装vmtools: 下载文件之后,到自动挂在的目录下(/run/media/用户名),将文件cp到其他的目录: 然后到其他的目录,解压缩,执行pl文件,执行方式:./vmware-install ...
- 表单传值给@Controller
<form action="springmvc/testModelAttributes" method="post"> <input type ...
- 记一次msyql导入导致的问题
公司有个项目要导入150M大小的sql文件,但是导入时报错,去网上找答案,很多人说是因为保留字什么什么的,所以就按照sql文件里面的mysql版本又去下载了一份mysql5.6安装好,但是登陆不了,又 ...
- 2—ARM中的异常中断
ARM体系中的3种控制程序执行的方式 正常执行过程中,每执行1条ARM指令,PC的值加4个字节:每执行1条Thumb指令,PC的值加2个字节.整个过程按照顺序执行. 通过跳转指令,调到特定的地址开始执 ...
- OpenGL的一些名词
搬运自:https://learnopengl-cn.github.io/01%20Getting%20started/10%20Review/ 词汇表 OpenGL: 一个定义了函数布局和输出的图形 ...
- pycharm中字体大小的调整方法
一.file->settings->editor->font->size 二.file中键入mouse,在其下editor->general->mouse选中:ch ...
- 一个切换内外网IP地址的批处理BAT
做了一个切换内外网的小脚本.没想到这个老的没剩几颗牙的DOS竟然功能如此强大.盛名之下名副其实啊!不亏是想当年叱咤风云的操作系统啊! 脚本内容1.建立两个TXT文件,分别按行存储内外网的IP,MASK ...
- Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
pycharm运行TensorFlow警告:Your CPU supports instructions that this TensorFlow binary was not compiled to ...