[LeetCode&Python] Problem 896. Monotonic Array
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.
Example 1:
Input: [1,2,2,3]
Output: true
Example 2:
Input: [6,5,4,4]
Output: true
Example 3:
Input: [1,3,2]
Output: false
Example 4:
Input: [1,2,4,5]
Output: true
Example 5:
Input: [1,1,1]
Output: true
Note:
1 <= A.length <= 50000
-100000 <= A[i] <= 100000
class Solution(object):
def isMonotonic(self, A):
"""
:type A: List[int]
:rtype: bool
"""
n=len(A)
if n==1:
return True
elif A[0]<A[1]:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
elif A[0]==A[1]:
ma=max(A)
if ma==A[0]:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]>A[i+1]:
return False
return True
else:
for i in range(n-1):
if A[i]<A[i+1]:
return False
return True
[LeetCode&Python] Problem 896. Monotonic Array的更多相关文章
- [LeetCode&Python] Problem 905: Sort Array By Parity
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- 896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] 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
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 697. Degree of an Array
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
随机推荐
- Chargen UDP服务远程拒绝服务攻击漏洞修复教程
一.前置说明 chargen服务最初设计用于测试网络状态,监听19端口(包括TCP和UDP),其中UDP协议存在“Chargen UDP服务远程拒绝服务攻击漏洞”. chargen一般不会使用,所以直 ...
- CentOS中/英文环境切换教程(CentOS6.8)
一.前言 对于不习惯英文的人可能想将系统由英文转成中文:而对于考虑客户端如果没正确配置,中文目录可能显示为乱码的人则可能宁愿将系统由中文转成英文. 中文切换为英文,实际就是将LANG的值由zh_CN- ...
- 命令创建Vue
创建vue+webpack vue init webpack projectName 基础 Vue+webpack+Vux 新建文件命令 # install vue-cli npm install - ...
- Oracle.数据的增删改、表操作(创建,修改,删除)、数据类型
SELECT ename,dname FROM emp,dept WHERE emp.deptno=dept.deptno; SELECT dname,loc FROM dept; SELECT JO ...
- unity中导入插件时报错处理办法
错误如下: Unhandled Exception: System.TypeLoadException: Could not load type 'System.ComponentModel.Init ...
- vs2015 产品密钥
一.破解秘钥 企业版 HM6NR-QXX7C-DFW2Y-8B82K-WTYJV 专业版 HMGNV-WCYXV-X7G9W-YCX63-B98R2 二.破解步骤 1.安装vs2015 2 ...
- day11- python生成式和生成器
列表生成式 列表生成式是python受欢迎的语法之一,通过一句简洁的语法就可以对一组元素进行过滤,还可以对得到的元素进行转换处理.语法格式为: [exp for val in collection i ...
- Netty完成网络通信(二)
Netty是基于NIO的框架,完善了NIO的一些缺陷,因此可以用Netty替代NIO Netty实现通信步骤: 1.创建两个NIO线程组,一个专门用于网络事件处理(接受客户端的连接),另一个则进行网络 ...
- JavaScript+CSS+DIV实现下拉菜单示例
<!DOCTYPE html> <html> <head> <title>下拉菜单示例</title> <script languag ...
- L255 Learning to say no brings a thrill of freedom
I am not sure who came up with that thing about never saying yes to something in the distant future ...