题目如下:

Given an array A of integers, return true if and only if it is a valid mountain array.

Recall that A is a mountain array if and only if:

  • A.length >= 3
  • There exists some i with 0 < i < A.length - 1 such that:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[B.length - 1]

Example 1:

Input: [2,1]
Output: false

Example 2:

Input: [3,5,5]
Output: false

Example 3:

Input: [0,3,2,1]
Output: true

Note:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000 

解题思路:非常简单的题目,没啥说的。

代码如下:

class Solution(object):
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
if len(A) < 3:
return False
last = None
directon = None
for i in A:
if last == None:
last = i
elif directon == None and last < i:
directon = 0
last = i
elif directon == None and last > i:
return False
elif directon == 0 and last < i:
last = i
elif directon == 0 and last > i:
directon = 1
last = i
elif directon == 1 and last > i:
last = i
else:
return False
return directon == 1

【leetcode】941. Valid Mountain Array的更多相关文章

  1. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode_easy】941. Valid Mountain Array

    problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...

  3. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  5. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

  8. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

  9. 【LeetCode】845. Longest Mountain in Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...

随机推荐

  1. Ubuntu14.04安装Ruby2.2方法

    直接使用系统的sudo apt-get install ruby2.0安装后,ruby -v显示ruby的版本依然是ruby 1.9. 以下方法可以顺序地在Ubuntu14.04安装Ruby2.2 s ...

  2. 分别在javascript和JSP中动态设置下拉列表默认值

    一.JavaScript中动态设置select标签中<option>选项的默认值: 比如,要完成下边这个下拉列表的动态显示,并且当进行前后翻页时,下拉列表中的值自动更新为当前页码: 图1 ...

  3. gitHub那些优秀的库和想要实现的效果

    1. 轮播库SDCycleScrollView 2. 自动布局库SDAutoLayout 3. 类似支付宝福卡滑动切换的效果 https://github.com/huangxuan518/HXCar ...

  4. spring-boot的helloWorld详解

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...

  5. hdu 6146 Pokémon GO (计数)

    Problem Description 众所周知,度度熊最近沉迷于 Pokémon GO. 今天它决定要抓住所有的精灵球!为了不让度度熊失望,精灵球已经被事先放置在一个2*N的格子上,每一个格子上都有 ...

  6. 【LeetCode 85】最大矩形

    题目链接 [题解] 把所有的"1"矩形分成m类. 第j类的矩形.他的右边界跟第j列紧靠. 那么. 我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1& ...

  7. 如何在vue里面调用高德地图

    1.修改webpac.base.conf.js文件 与module同一级添加 externals: { 'AMap': 'AMap', 'AMapUI': 'AMapUI' }配置. 然后在index ...

  8. STL中的查找

    一.查找 1.头文件 #include <algorithm> 2.使用方法 1.binary_search:查找某个元素是否出现.O(logn) a.函数模板:binary_search ...

  9. Honk's pool(二分模板题)

    题意:有n个水池,每个水池有a[i]单位水,有k次操作,每次操作将水量最多的水池减少一单位水,水量最少的水池增加一单位水,问最后水量最大的水池和水量最少的水池相差的水量. 思路:二分最后的最大水量和最 ...

  10. python学习笔记:try与except处理异常语句

    写代码的时候会遇到各种各样的异常,那么代码就不会继续往下走了.比如说10除以0是错误的,因为除数不能为零学会捕捉异常,在异常出现的时候我们要做什么操作. 本文中只做简单使用的讲解,详细使用方法可以参考 ...