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 是单调 ...
随机推荐
- HDU 1713 相遇周期 (最小公倍数)
题意:... 析:求周期就是这两个分数的最小公倍数,可以先通分,再计算分子的最小倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,10 ...
- CentOS 7 设置系统语言为英文并解决 cannot change locale 问题
首次安装Cent OS 7.6时,将系统语言设置成了中文.后续学习和使用过程中却发现种种不便,甚至有翻译错误.为锻炼自己的英文能力,所以将系统语言设置问英文. 编辑 locale 配置文件,将 LAN ...
- MYSQL性能调优与架构设计之select count(*)的思考
select count(*)的思考 原文:MYSQL性能调优与架构设计 举例: 这里我们就拿一个看上去很简单的功能来分析一下. 需求:一个论坛帖子总量的统计 附加要求:实时更新 在很多人看来,这 ...
- android 启动报错
报错如下: AAPT err(Facade for 1532009679): libpng error: Read Error Error:Execution failed for task ':ap ...
- JSP | 基础 | 两种方式循环输出
用循环连续输出三个你好,字体从小变大 第一种: <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = "&qu ...
- Educational Codeforces Round 46 (Rated for Div. 2) A. Codehorses T-shirts
Bryce1010模板 http://codeforces.com/problemset/problem/1000/A 题意: 问你将一种类型的衣服转换成另一种的最小次数. #include<b ...
- 洛谷 P2023 [AHOI2009]维护序列 || 线段树加法和乘法运算
原理倒是非常简单.设原数为x,加法的lazytag为b,乘法的lazytag为a,操作数为c,那么原式为ax+b,乘上c后(ax+b)c=(ac)*x+b*c,加上c后(ax+b)+c=ax+(b+c ...
- cordova 安卓项目打包 release安装包
问题描述: 打包安卓项目, 如果是在项目中只是使用debug包的话, 其中的签名方式使用的都是cordova框架本身, 那么每次打包的话, 都会把之前的安装包给覆盖掉. 现在打包做出一个release ...
- yii2 checkbox 的使用实例
使用的是Yii自带的Grid,在使用checkbox希望可以把选中行的id值传到我想要的页面. 首先需要改变key值 $dataProvider->key = 'ID'; button: Htm ...
- Oracle JDK各版本下载地址记录
Oracle JDK各版本下载地址: https://www.oracle.com/technetwork/java/javase/archive-139210.html