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 some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
Example 1:
Input: [0,1,0]
Output: 1
Example 2:
Input: [0,2,1,0]
Output: 1
Note:
- 3 <= A.length <= 10000
- 0 <= A[i] <= 10^6
- A is a mountain, as defined above.
二分法:
class Solution{
public:
int peakIndexInMountainArray(vector<int>& a){
int beg = ,end = a.size();
int mid = (beg + end) / ;
while(beg <= end){
if(a[mid] < a[mid - ]){
end = mid - ;
}
else if(a[mid] < a[mid + ]){
beg = mid + ;
}
else{
break;
}
mid = (beg + end) / ;
}
return mid;
}
};
最大值法
class Solution{
public:
int peakIndexInMountainArray(vector<int>& a){
int max_elem = *max_element(a.begin(), a.end());
int pos;
for(pos = ; pos < a.size(); ++pos){
if(a[pos] == max_elem)
break;
}
return pos;
}
};
LeetCode 852. Peak Index in a Mountain Array(C++)的更多相关文章
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- 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
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/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- LeetCode算法题-Peak Index in a Mountain Array(Java实现)
这是悦乐书的第329次更新,第352篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第199题(顺位题号是852).如果以下属性成立,我们将数组A称为山: A.length ...
- 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 ...
随机推荐
- break语句、continue语句、goto语句的用法辨析
1.break语句 break语句常使用在switch语句.循环体以及if语句中,它的作用是跳出循环,而且只能跳出一层循环. for (i = 0; i < 10; ++j) { for (j ...
- ELK Stack
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11488404.html ELK workflow log -> filebeat -> l ...
- vsto c# 获取word里面的图片并保存
internal void GetEmbeddedImages() { ; Document doc = Globals.ThisAddIn.Application.ActiveDocument; f ...
- deque的简单使用
depue 是python提供的一个数据结构,线程安全,功能比list强大 from collections import deque user_list = ['admin', 'root'] us ...
- php array_intersect()函数 语法
php array_intersect()函数 语法 作用:用于比较两个(或更多个)数组的键值,并返回交集.直线电机生产厂家 语法:array_intersect(array1,array2,arra ...
- App.after
解释: App.after可以增加APP级的切面,触发的时机是在所拦截的对应生命周期方法执行之后. 方法参数:Object Object 参数说明: 参数名 类型 必填 默认值 说明 methods ...
- php怎么获取js的变量值
使用php做网站的时候,经常需要我们与前端的页面进行交互,有时候我们还需要通过php来获得js变量中的值,这种情况我们可以通过在其中嵌入js代码的方式来获得这个变量. 首先我们创建一个test的php ...
- ionic学习使用笔记(二) slide 组件的使用
开始做的时候,遇到了个要用ionic实现 有一系列的序列需要展示,但是当前页面上只能展示一小部分,剩余的在没有出现时是隐藏的,还得能滑动出现,但是又不能有滚动条. 之前使用jQuery来实现的话,其实 ...
- win10下VMware15运行ubuntu18.04无法和主机之间复制粘贴问题
可以运行以下命令行: sudo apt-get autoremove open-vm-tools sudo apt-get install open-vm-tools sudo apt-get ins ...
- xpath使用技巧
爬虫中我们对于元素的定位有多种方法,大致有: Beautifulsoup.Xpath和正则表达式三种方式 其中效率比较为: Beautifulsoup<Xpath<正则表达式 习惯了使用B ...