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. 跳马~~~HDU1372

    基础BFS,水过就好~手写队列优化~~ #include <iostream> #include <stdio.h> #include <string.h> #in ...

  2. C#拷贝整个文件夹以及子目录和其中文件

       private void CopyDirectory(string srcPath, string desPath)         {             string folderNam ...

  3. SpringBoot的配置文件加载顺序以及如何获取jar包里的资源路径

    一.读取配置文件的四种方式 这四种配置文件放置方式的读取优先级依次递减,具体可以查看官方文档. 1.1jar包同级目录下的config文件夹里的配置文件 其实我以前就见过这种方式了,只是不知道怎么做的 ...

  4. 统计Apache或nginx日志里访问次数最多的前十个IP

    1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...

  5. c++ const的使用

    const是用来声明一个常量的,当你不想让一个值被改变时就用const,const int max && int const max 是没有区别的,都可以.不涉及到指针const很好理 ...

  6. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  7. 洛谷 P1072 Hankson 的趣味题 || 打质数表的分解质因数

    方法就是枚举,根据b0和b1可以大大减小枚举范围,方法类似这个http://blog.csdn.net/hehe_54321/article/details/76021615 将b0和b1都分解质因数 ...

  8. 当css样式表遇到层2

    9.定制层的display属性:层的表现是通过框这种结构来实现的.框可以是块级对象也可以是行内对象. Display属性就是用来控制其中内容是块级还是行级.定义为block则为kuai块级,inlin ...

  9. C#基础学习5

    数组集合

  10. MyBatis -- 必知必会

    MyBatis的前身是Apache的一个开源项目iBatis,2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis.201 ...