[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 ...
随机推荐
- ASP.NET MVC命名空间时引起错误的解决方法
使用VS2012新建了一个Asp.net mvc5的项目,并把项目的命名空间名称更改了(Src更改为UXXXXX),然后就导致了以下错误 刚开始以后是项目的属性中的命名空间没有更改过来的问题,但我在重 ...
- 01二维矩阵中最大全为1的正方形maxSquare——经典DP问题(二维)
在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...
- Qt_自定义菜单
一.右键菜单 右键菜单实现:通过重写contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美实现! 重写voidcontextMen ...
- vul/0day/shellcode/payload/poc/exp
vul--泛指漏洞 0day--未公开或虽已公开但还没有修复方法的漏洞 shellcode--远程溢出后执行的那段代码 payload--攻击载荷,送到远端机器执行的整段代码 poc--Proof o ...
- linux git:fatal: HTTP request failed
问题 问题的出现比较奇怪 我一台电脑 git clone 没问题 另外一台电脑 git clone 有问题 解决 yum update nss nss-util nspr 参考 https: ...
- mac crontab时间断内随机时间执行定时任务
首先需要了解crontab使用,这里不多,主要是时间断内随机时间: 然而crontab 并没有具体方法实现时间段内随机时间执行,我的办法如下: 这里测试一个例子: 执行一个数据存文件python脚本, ...
- ID基本操作(创建主页,复制主页,把主页应用到多个页面)5.11
主页上的对象将会显示在应用在这个主页上的所有页面. 一.创建主页的方法: 1.页面面板,右上方点击,可以新建主页..前缀:用来识别页面面板中的各个页面所应用的主页.最多可输入四个字符.名称:输入主页跨 ...
- Win10系列:C#应用控件基础3
CheckBox控件 在应用程序的开发过程中开发者经常使用一组CheckBox控件来显示多个复选框,让用户从中选择一个或多个.当用户勾选复选框后,被选中的复选框会被标记为勾选状态,再次点击此复选框可取 ...
- Java反射《二》获取构造器
package com.study.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Invocation ...
- MATLAB图片折腾3
把视频抽帧,转化成图片 我的代码如下,成功实现clc;clear;videofilename='k:\GraduationWork\Resource\video.wmv'; %where you pu ...