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 exists some
0 < i < A.length - 1such thatA[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].
题目分析及思路
题目给出一个整数数组,满足数组长度大于等于3且存在一个元素,它前面的元素依次递增,后面的元素依次递减。要求返回该元素的的索引。遍历数组,当某元素大于后一个元素则该元素即为所求元素。
python代码
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
l = len(A)
for i in range(1,l):
if A[i-1]>A[i]:
return i-1
LeetCode 852 Peak Index in a Mountain Array 解题报告的更多相关文章
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- 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(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 ...
- 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】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Current online Redo 和 Undo 损坏的处理方法
转自:http://blog.csdn.net/tianlesoftware/article/details/6261475 Oracle 不同故障的恢复方案 http://blog.csdn.net ...
- 8个超实用的jQuery插件应用
自jQuery诞生以来,jQuery社区都在不断地.自发地为jQuery创建许许多多功能不一的插件应用,很多jQuery插件非常实用,对我们的前端开发帮助相当大,不仅可以更完美的完成指定功能,而且节省 ...
- js实现cookie跨域功能
/** * 设置cookie方法 * @param {string} name cookie键值 * @return {*} 返回cookie值 */ function setCookie_log(c ...
- Spring 源码学习:day1
前言: 最近也不知道该忙些什么样的事情.便随便看看源码算了. 正文: (1) 在网上下载 Spring 的源码: 可以采用 git 方式下载 https://github.com/spring-pro ...
- 【Mac brew】代理安装brew insall
http_proxy=dev-proxy.**.**:8080 https_proxy=dev-proxy.**.**:8080 brew install npm
- LRU 算法
LRU算法 很多Cache都支持LRU(Least Recently Used)算法,LRU算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定 ...
- Fidder
第一步:下载Fiddler,下载链接: http://fiddler2.com/get-fiddler 下载完成之后,傻瓜式的安装一下了! 第二步:设置Fiddler 打开Fiddler, Tools ...
- Mac 安装HomeBrew 出错
错误如下: error: could not lock config file /usr/local/Homebrew/.git/config: Permission denied fatal: co ...
- MySQL 删除数据库中重复数据方法
1. 查询需要删除的记录,会保留一条记录. select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RE ...
- 字符集和编码——Unicode(UTF&UCS)深度历险
计算机网络诞生后,大家慢慢地发现一个问题:一个字节放不下一个字符了!因为需要交流,本地化的文字需要能够被支持. 最初的字符集使用7bit来存储字符,因为那时只需要存下一些英文字母和符号.后来虽然扩展到 ...