作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/valid-mountain-array/description/

题目描述

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

题目大意

判断一个数组是不是山形数组,山形数组最少有3个数字,中间有个最大的数字,往两边都是依次减小的。

解题方法

方法很直接,先判断是不是依次增加,然后再判断是不是依次减小,如果整个数组都是这样的就是山形数组了。

所以有两个while循环,一个是向后查找更大的数字,第二个是向后找最小的数字。在第一个while结束的时候,找到的元素的位置应该在数组中间,而第二个while结束之后,元素的位置应该在数组的结尾。

时间复杂度是O(N),空间复杂度是O(1)。

class Solution:
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
N = len(A)
if N < 3:
return False
i = 0
while i < N - 1:
if A[i] < A[i + 1]:
i += 1
else:
break
if i == 0 or i == N - 1: return False
while i < N - 1:
if A[i] > A[i + 1]:
i += 1
else:
break
return i == N - 1

其实while的条件也可以使用判断语句,这样的话,我们就直接停止。

class Solution(object):
def validMountainArray(self, A):
"""
:type A: List[int]
:rtype: bool
"""
print(A)
N = len(A)
if N < 3: return False
i = 0
while i < N - 1 and A[i + 1] > A[i]:
i += 1
if i == 0 or i == N - 1: return False
while i < N - 1 and A[i] > A[i + 1]:
i += 1
return i == N - 1

日期

2018 年 11 月 18 日 —— 出去玩了一天,腿都要废了
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】941. Valid Mountain Array 解题报告(Python)的更多相关文章

  1. LeetCode 941. Valid Mountain Array (有效的山脉数组)

    题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...

  2. 【Leetcode_easy】941. Valid Mountain Array

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

  3. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  4. 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...

  5. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  6. 【LeetCode】525. Contiguous Array 解题报告(Python & C++)

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

  7. 【LeetCode】896. Monotonic Array 解题报告(Python)

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

  8. 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 ...

  9. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

随机推荐

  1. 如何鉴定全基因组加倍事件(WGD)

    目前鉴定全基因组加倍(whole-genome duplication events)有3种 通过染色体共线性(synteny) 方法是比较两个基因组的序列,并将同源序列的位置绘制成点状图,如果能在点 ...

  2. miRAN 分析以及mRNA分析

    一些参考资料 http://www.360doc.com/content/17/0528/22/19913717_658086490.shtml https://www.cnblogs.com/tri ...

  3. MariaDB—备份数据库

    1> 备份单个数据库 mysqldump -uroot -plichao123 --database students1 > stundents.sql; 2>查看备份文件 3> ...

  4. 阿里云NAS性能测试

    测试方法:根据阿里云NAS官方文档进行测试 测试对象:性能型NAS,总容量1PB,已使用27.49GB(计算吞吐量时按30GB计算) 随机读IOPS测试 测试命令 fio -numjobs=1 -io ...

  5. 日常Java 2021/10/20

    Java提供了一套实现Collection接口的标准集合类 bstractCollection 实现了大部分的集合接口. AbstractList 继承于AbstractCollection并且实现了 ...

  6. Mapreduce中的join操作

    一.背景 MapReduce提供了表连接操作其中包括Map端join.Reduce端join还有半连接,现在我们要讨论的是Map端join,Map端join是指数据到达map处理函数之前进行合并的,效 ...

  7. Scala(一)【安装和IDEA中开发】

    目录 一.下载 二.windows安装 三.linux环境安装 四.Ida开发Scala 1.在线下载Scala插件 2.离线下载Scala插件 3.验证 五.HelloWorld入门程序 1.新建M ...

  8. Maven 目录结构[转载]

    转载至:http://www.cnblogs.com/haippy/archive/2012/07/05/2577233.html Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目, ...

  9. Linux基础命令---host域名查询工具

    host host是一个常用的DNS查询工具,经常用来查询域名.检查域名解析是否正确. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       ...

  10. 3.6 String 与 切片&str的区别

    The rust String  is a growable, mutable, owned, UTF-8 encoded string type. &str ,切片,是按UTF-8编码对St ...