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. hdoj3183【思维】

    思路: 处理方案非常霸气啊,无奈想不到. 说是n位去m个,那么默认就是取了n-m个数字,ok,然后m #include <iostream> #include <stdio.h> ...

  2. POJ1466/HDOJ1068 谈谈二分匹配的时间复杂度

    题意: n个学生编号是0-n-1,然后给出他们这些人和一些人的关系. 要你找出最大的人数,要求这些人之间没关系. 咳咳,题目中没说性别的关系TVT 思路: 很明显是一个匹配算法,但是我们得搞出一个二分 ...

  3. linux 问题一 apt-get install 被 lock

    问题: sudo apt-get install vim E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporari ...

  4. bzoj 3667: Rabin-Miller算法【Miller-Rabin】

    Miller-Rabin模板 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  5. springcloud(二) 负载均衡器 ribbon

    代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo ribbon是一个负载均衡客户端 类似nginx反向代理,可 ...

  6. 黑客攻防技术宝典web实战篇:攻击访问控制习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 一个应用程序可能通过使用 HTTP Referer 消息头实施访问控制,但它的正常行为并没 ...

  7. 调试的时候禁止chrome缓存图片

    https://blog.csdn.net/yiifaa/article/details/54290047 https://blog.csdn.net/xinghuo0007/article/deta ...

  8. SOA架构设计和相关案例分析

    一.SOA概念 1.定义: SOA,是一个组件模型,面向服务的体系架构,它将应用程序的不同服务通过这些服务之间定义良好的接口和契约联系起来,不涉及底层编程接口和通讯模型.服务层是SOA的基础,可以直接 ...

  9. UvaLive6442(思维、结论)

    结论是:按位置排序好以后,对于真正的答案,走法应该是:依次走向第0个等分点,第1个等分点……这样对于这种等分情况,是最优的调度. /* 先假设一个终点位置然后按位站好 这个位置不一定是最优所以要调 调 ...

  10. Hibernate Could not obtain transaction-synchronized Session for current thread问题处理

    项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...