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.

原题:leetcode 896.Monotonic Array

题意:判断一个数组是否是单调的

1.判断单调性:数组值比较

存在三种情况:

(1)A[0] > A[-1]: 单调递减

(2)A[0] < A[-1]: 单调递增

(3)A[0] == A[-1]: 所有值全相等

2.遍历数组,验证数组是否单调递增或者单调递减或者不变

代码如下:

class Solution(object):
def isMonotonic(self, A):
"""
:type A: List[int]
:rtype: bool
"""
n = len(A)
i = 0
if A[0] < A[n-1]: # 单调递增
while i < n-1:
if A[i] <= A[i+1]:
i += 1
else:
return False
return True
elif A[0] > A[n-1]: # 单调递减
while i < n-1:
if A[i] >= A[i+1]:
i += 1
else:
return False
return True
else: # 不变
while i < n-1:
if A[i] == A[i+1]:
i += 1
else:
return False
return True

时间复杂度: O(n)

空间复杂度: O(1)

896. Monotonic Array@python的更多相关文章

  1. 【Leetcode_easy】896. Monotonic Array

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

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

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

  3. [LeetCode&Python] Problem 896. Monotonic Array

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

  4. LeetCode 896 Monotonic Array 解题报告

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

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

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

  6. 896. Monotonic Array单调数组

    [抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...

  7. 896. Monotonic Array

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

  8. LeetCode 896. Monotonic Array

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

  9. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

随机推荐

  1. hdoj2795【未完待续】

    题意: 给你一个矩形h*w(h,w都是1e9),然后给你n个询问,每个询问带一个1*wi矩形,问你这个给定矩形能放在第几行(从1-h下来).如果最终放不下了,就输出-1. 比如案例: 3 5 5 2 ...

  2. python 通过setup.py安装和卸载python软件包

    安装:sudo python setup.py install 卸载:sudo python setup.py install --record log sudo cat log | sudo xar ...

  3. poj 1743 Musical Theme【二分+SA】

    差分,然后二分长度mid,判断是把height按照min不小于mid分组,取最大最小的sa位置看是否>=mid即可,注意差分后最后答案要+1 #include<iostream> # ...

  4. 手机测试用例-STK测试用例

    ID 功能描述 操作步骤 预期结果 test time P/F comment tester test time P/F comment tester STK服务 SIM卡适应性测试 1.选取支持ST ...

  5. jQuery 第九章 工具方法

    $.type() $.isArray() $.isFunction() $.isWindow()... $.trim() $.proxy() $.noConflict() $.each() $.map ...

  6. Nginx系列篇三:linux中Nginx+keepalived做一个高可用的主从配置

    建议:先阅读搭建Nginx负载均衡之后再看此篇 备注: Nginx+keepalived的高可用有两种方式 一.主从配置 二.双主热备配置[下一篇] 准备: 标配四台服务器 Master:192.16 ...

  7. 跟我一起玩Win32开发(20):浏览文件夹

    最近忙于一些相当无聊的事情,还没忙完,不过,博客还是要写的,不然我头顶上会多了几块砖头. 在上一篇博文中,我们浏览了文件,今天我们也浏览一下目录,如何? 浏览目录我们同样有两个规矩,用托管类库的我就不 ...

  8. loj125 除数函数求和 2

    https://loj.ac/problem/125 $原式=2\sum_{i=1}^n(i^2*{\lfloor}{\frac{n}{i}}{\rfloor})+3\sum_{i=1}^n(i*{\ ...

  9. 常用的DOCS命令

    1.Help 可以查看当前DOS常用命令,是帮助2.Help dir 查看Dir命令的帮助,使用帮助3.ipconfig 查看当前电脑的IP地址4.ping 127.0.0.1 测试与某一台电脑之间网 ...

  10. vmware虚拟机启动centOs黑屏

    如图所示  , 我的VM 启动虚拟机之后就变成了上面的样子,一直不动,ping也ping不好,这个时候 : 1. 要么 内存不够了: 2. 要么 网络协议存在问题了: 本地windows环境在管理员的 ...