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 ...
随机推荐
- django中collectstatic的使用
前言 我最近在琢磨django框架的使用,在上传个人网站服务器上时,再次遇到了找不到静态文件,css.img等样式全无的问题.于是沉下心来,好好研究了django的静态文件到底应该怎么去部署(depl ...
- 程序员要注意!现在是RSS复兴的时候了
一般来说,现代网络不乏恐怖,从无所不在的网络黑客到所有信息平台,再到各大平台的评论系统.不幸的是,我们建立的这个互联网并没有什么灵丹妙药.但任何人都厌倦了黑箱算法,控制你在网上看到的东西,一直存在但始 ...
- php array_chunk()函数 语法
php array_chunk()函数 语法 作用:把数组分割为新的数组块.dd马达参数 语法:array_chunk(array,size,preserve_key) 参数: 参数 描述 array ...
- git使用技巧总结
一.把github上的代码下载下来 git clone ...... 二.把修改的代码提交上去 git add .... git commit -m "........" git ...
- tensorflow2 keras.Callback logs
官方文档上表示logs内存的内容为 on_epoch_end: logs include `acc` and `loss`, and optionally include `val_loss` (if ...
- php strtotime,mktime,DateTime函数处理时间累加问题
时间戳(年月日时分秒) 使用strtotime函数,结合+1 month,-1 month,next month,last month的时候会出现一些问题. demo示例: //时间"20 ...
- Flask学习 4 http方法
服务端端 #!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: f ...
- 牛客提高D1t2 最小生成链
分析 我们发现可以把题目转化为:有一个序列a,问它的排列中相邻两个值异或的最大值的最小值 我们发现序列的构成一定是前几位全是一样的 从某一位开始左面全是0右面全是1 所以只要找到一种方案是的交界两个值 ...
- 刚性方程 Stiff equation
In mathematics, a stiff equation is a differential equation for which certain numerical methods for ...
- 杂项-SAP:SAP (服务访问点(Service Accessing point))
ylbtech-杂项-SAP:SAP (服务访问点(Service Accessing point)) 1.返回顶部 1. SAP,是Service Accessing point的缩写,意思是服务访 ...