【LeetCode】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛
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
iwith0 < i < A.length - 1such 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:
0 <= A.length <= 100000 <= 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)的更多相关文章
- LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...
- 【Leetcode_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- 【leetcode】941. Valid Mountain Array
题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
随机推荐
- 数据集成工具—FlinkX
@ 目录 FlinkX的安装与简单使用 FlinkX的安装 FlinkX的简单使用 读取mysql中student表中数据 FlinkX本地运行 MySQLToHDFS MySQLToHive MyS ...
- A Child's History of England.50
'Knave [man without honor]!' said King Richard. 'What have I done to thee [you] that thou [you] shou ...
- The Ultimate Guide to Buying A New Camera
[photographyconcentrate] 六级/考研单词: embark, thrill, excite, intimidate, accessory, comprehensive, timi ...
- 重磅丨腾讯云开源业界首个 etcd 一站式治理平台 Kstone
Kstone 开源 在 CNCF 云原生基金会举办的2021年12月9日 KubeCon China大会上,腾讯云容器 TKE 团队发布了 Kstone etcd 治理平台开源项目. Kstone ...
- [php安全]原生类的利用
php原生类的利用 查看原生类中具有魔法函数的类 $classes = get_declared_classes(); foreach ($classes as $class) { $methods ...
- 『学了就忘』Linux启动引导与修复 — 70、grub启动引导程序的配置文件说明
目录 1.grub中分区的表示方法 2.grub的配置文件 3.grub的配置文件内容说明 (1)grub的整体设置 (2)CentOS系统的启动设置 1.grub中分区的表示方法 在说grub启动引 ...
- Oracle—表、约束、索引、表空间、分区、序列、统计信息
表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...
- JDBC(3):PreparedStatement对象介绍
一,PreparedStatement介绍 PreperedStatement是Statement的子类,它的实例对象可以通过Connection.preparedStatement()方法获得,相对 ...
- 开源低代码开发平台entfrm2.1.0更新
开源低代码开发平台entfrm2.1.0更新 新功能 代码生成支持主子表,支持预览: 新增多应用顶部菜单与左侧菜单联动: element-ui升级到2.15.1: 新增表单管理,集成avue-from ...
- java实现数组集合转成json格式
一.下载fastjson.jar http://repo1.maven.org/maven2/com/alibaba/fastjson 二.项目添加jar包 Java Build Path 三.导入类 ...