896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[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的更多相关文章
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- [LeetCode] 896. Monotonic Array 单调数组
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 896. Monotonic Array单调数组
[抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...
- 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode 896. Monotonic Array
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
- LeetCode 896. 单调数列(Monotonic Array)
896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...
随机推荐
- NYOJ4——ASCII码排序
ASCII码排序 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述:输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入:第一行输入一 ...
- 51nod 1272【二分+RMQ】
思路: 这题不能说是长见识,倒是第一次写这么富有套路的题,倒着来,二分区间嘛,这个很简单啊,二分的条件查询一个当前区间的最小值是不是比那个特定的值小,一步步缩小,这就是二分嘛,然后查询用线段树的RMQ ...
- python help(int)
class int(object) | int(x=0) -> integer | int(x, base=10) -> integer | | Convert a number or s ...
- C#批量插入Sybase数据库,Anywhere 8
数据库版本是Adaptive Server Anywhere 8 1.添加引用,程序集 iAnywhere.Data.AsaClient.这个和SQLServer的System.Data.SqlCli ...
- 【Nginx】解决Post请求变Get的问题
默认情况下Nginx会把post请求做一次重定向操作,然后后端收到的就成了Get请求,还会导致一些参数的遗漏. 日志如下: 172.16.1.108 - - [11/Jan/2019:18:27:09 ...
- Hexo - 修改永久链接的默认格式
Hexo的永久链接的默认格式是 :year/:month/:day/:title/,比如访问站点下某一篇文章时,其路径是 2018/04/12/xxxx/,如果我们的文章标题是中文的,那么该路径就会出 ...
- IE css hack整理
CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozillafirefox等,对CSS的解析认识不一样,因此会导致生成的页面效果 ...
- Python入门小练习 002 批量下载网页链接中的图片
我们常常需要下载网页上很多喜欢的图片,但是面对几十甚至上百张的图片,一个一个去另存为肯定是个很差的体验. 我们可以用urllib包获取html的源码,再以正则表达式把匹配的图片链接放入一个list中, ...
- Easy Game LightOJ - 1031
Easy Game LightOJ - 1031 upd:似乎有复杂度更优越的做法,见http://www.cnblogs.com/hehe54321/p/8431020.html 题意:A和B玩一个 ...
- 522 Longest Uncommon Subsequence II 最长特殊序列 II
详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...