An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[i] >= A[j].

Return true if and only if the given array A is monotonic.

Example 1:

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

Example 2:

Input: [6,5,4,4]
Output: true

Example 3:

Input: [1,3,2]
Output: false

Example 4:

Input: [1,2,4,5]
Output: true

Example 5:

Input: [1,1,1]
Output: true

Note:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000
 
class Solution(object):
def isMonotonic(self, A):
"""
:type A: List[int]
:rtype: bool
"""
n=len(A)
if n==1:
return True
elif A[0]<A[1]:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
elif A[0]==A[1]:
ma=max(A)
if ma==A[0]:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True

  

[LeetCode&Python] Problem 896. Monotonic Array的更多相关文章

  1. [LeetCode&Python] Problem 905: Sort Array By Parity

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

  2. 【Leetcode_easy】896. Monotonic Array

    problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...

  3. 896. Monotonic Array@python

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

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

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

  5. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  6. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

  7. LeetCode 896. Monotonic Array

    原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...

  8. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [LeetCode&Python] Problem 697. Degree of an Array

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

随机推荐

  1. [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

    描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...

  2. 使用python操作hdfs,并grep想要的数据

    代码如下: import subprocess for day in range(24, 30): for h in range(0, 24): filename = "tls-metada ...

  3. Oracle常用表和常见操作命令

    一.说明 Oracle数据库数据库名.表名.字段名等不区分大小写,字段值区分大小写. Oracle单词之间一般用下划线连接:表名最后一般加s字段名最后一般没s. 二.常见数据表 dba_*DBA拥有的 ...

  4. java下使用chromedriver获取访问页面状态码

    ##在使用chromedriver的时候 并没有提供api来获取访问页面的状态码,但是可以打开日志来获取到 LoggingPreferences logPrefs = new LoggingPrefe ...

  5. charles抓包工具使用方法

    注意: 1.软件安装证书,移动端安装证书: 2.设置SSL proxying setting: 3.设置Map Remote: 这里主要是为了设置移动端代理服务器,让其重定向到想要连接的环境上,比如手 ...

  6. Linux下使用systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...

  7. suffix word cy dom faction ful fold form out1

    1★cy ﹎﹎﹎﹎性质,状态 2★ dom ﹎﹎﹎﹎领域,范围     1★ faction ﹎﹎﹎﹎达到什么样的状态 2★ ful ﹎﹎﹎﹎满,量 *******有~的 3★ fold ﹎﹎﹎﹎双, ...

  8. shell脚本学习之参数传递

    shell之参数传递 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以 ...

  9. Java Web(十一) 文件上传与下载

    文件上传 上传的准备工作 表单method必须为post 提供file组件 设置form标签的enctype属性为multipart/form-data,如果没有设置enctype属性,浏览器是无法将 ...

  10. 对大学生学习Linux系统的七项实用建议

    你现在的工作是你所渴望的理想工作吗?或者说这只是你整个职业生涯中的一段插曲?虽然我们每个人都不一定能够说出自己所想的是什么,但是我们心里其实跟明镜似的.相信许多人对于自己喜好的工作投入精力不会有问题, ...