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 ...
随机推荐
- [USACO]奶牛会展(背包)
[USACO]奶牛会展 题目背景 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行 了面试,确定了每头奶牛的智商和情商. 题目描述 贝西有权选择让哪些奶牛参加展览. ...
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- thinkphp 模板变量输出替换和赋值
一.变量输出的几个方法 <?php namespace app\index\controller; use http\Params; use think\Config; use think\Co ...
- kafka docker-composer.yml
使用Docker快速搭建Kafka开发环境 表现力 关注 0.5 2018.05.04 03:00* 字数 740 阅读 25240评论 1喜欢 11 Docker在很多时候都可以帮助我们快速搭建想 ...
- TCP TIME_WAIT和CLOSE_WAIT
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484451.html 使用如下指令查看当前Server的TCP状态 netstat -n | awk ...
- 【HDOJ6630】permutation 2(递推)
题意:给定x,y,n,有标号从1到n的n个数组,求合法的排列个数模998244353使得 1:p[1]=x 2:p[n]=y 3:相邻两项的差的绝对值<=2 n<=1e5 思路: #inc ...
- Number theory
题目链接 思路:针对一个数组的操作,即对一个区间.可以用线段树去进行维护.初始化建树,叶子节点的值为1,维护每段区间上各个元素的乘积sum.M yi,将第i个元素的值改为yi.N di,将第di个元素 ...
- python发送消息到ipmsg
from socket import * #利用socket模块生成套接字s = socket(AF_INET,SOCK_DGRAM) #定义一个元组,包含ip地址,和端口号,ip地址必须为字符串,端 ...
- ArayList的一些常用法<一> 转
转:http://yy.azj.blog.163.com/blog/static/18508700320122893451389/ import java.util.ArrayList; import ...
- HDU4035 Maze (概率DP)
转:https://www.cnblogs.com/kuangbin/archive/2012/10/03/2711108.html 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, ...