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 是单调 ...
随机推荐
- 家庭wifi,如何组网最合适
wifi信号通过电磁波在空中传播的,属于微波通信的一种,因为微波本身及发射功率的限制,导致wifi的穿透能力比较差,北方比较厚的承重强,铁门.家具等对都会对wifi信号有较强的削弱作用.穿过的障碍物越 ...
- EOJ3247:铁路修复计划
传送门 题意 分析 这题用二分做就好啦,有点卡常数,改了几下for的次数 套了个板子,连最小生成树都忘记了QAQ trick 代码 #include<cstdio> #include< ...
- bzoj 1396: 识别子串 && bzoj 2865: 字符串识别【后缀数组+线段树】
根据height数组的定义,和当前后缀串i最长的相同串的长度就是max(height[i],height[i+1]),这个后缀贡献的最短不同串长度就是len=max(height[i],height[ ...
- SVG新手入门
特点 矢量图 属性:形状的参数(都没有单位) 添加事件跟html一样 修改样式跟html一样 属性操作: setAttribute/getAttribute 图形 <svg width=&quo ...
- pycharm 断点调试
转自; https://blog.csdn.net/chenggong2dm/article/details/9368641 PyCharm 作为IDE,断点调试是必须有的功能.否则,我们还真不如用纯 ...
- JQUERY 获取 DIV 宽度与高度(width,padding,margin,border)
一般讲的宽度指的是内容宽度,但一个 div 的实际宽度不仅只于内容宽度,尤其在做 CSS 排版时更不能搞错,必须同时考虑 Padding.Border 与 Margin 的宽度,四个加起来才是 di ...
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- 创建表的规范 nvarchar2,varchar2
1,这个真没见过什么最佳实践,都是变长的,这些都是研发根据业务需求自己设定啊. 如果需要多语种支持就用NVARCHAR2(或者汉语),如果只是单语种(英语)就varchar2 . 2. Oracle中 ...
- vscode中将本地数据push至git repository
1.新建repository 2.本地写好的代码 3.执行git init 初始化git配置文件 4.提交已暂存文件 5.填写提交信息 6.执行push命令 7.完成
- AJPFX总结String类的特点
String str = "abc"; str就是String的一个对象 字符串一旦被赋值, 值就不能再被改变了 举例:String s ...