【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 ...
随机推荐
- mGWAS研究思路
mGWAS(metabolome-based genome-wide association studies)的研究方法和GWAS类似,只是将代谢物含量作为表型性状来与基因位点进行关联.但同时它也有自 ...
- perl中tr的用法(转载)
转载:http://blog.sina.com.cn/s/blog_4a0824490101hncz.html (1)/c表示把匹配不上的字符进行替换. $temp="AAAABCDEF&q ...
- 57-Palindrome Linked List
Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Submiss ...
- 自定义char类型字符,django中事务
自定义char类型字符 # 自定义char类型,继承Field父类 class MyCharField(Field): def __init__(self, max_length, *args, ** ...
- urllib的基本使用介绍
1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...
- Go语言缺陷
我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...
- Java事务与JTA
一.什么是JAVA事务 通俗的理解,事务是一组原子操作单元,从数据库角度说,就是一组SQL指令,要么全部执行成功,若因为某个原因其中一条指令执行有错误,则撤销先前执行过的所有指令.更简答的说就是:要么 ...
- 3.5 Rust Generic Types, Traits, and Lifetimes
Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...
- Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...
- 收集linux网络配置信息的shell脚本
此脚本已在CentOS/ RHEL和Fedora Linux操作系统下进行测试过.可用于当前网络配置信息. 代码: #!/bin/bash # HWINF=/usr/sbin/hwinfo IFCFG ...