这是悦乐书的第345次更新,第369篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第210题(顺位题号是896)。如果数组单调递增或单调递减,则数组是单调的。如果对于所有i <= j,A[i] <= A[j],则数组A是单调递增的。如果对于所有i <= j,A[i] >= A[j],则数组A是单调递减的。当且仅当给定的数组A是单调的时,才返回true。例如:

输入:[1,2,2,3]

输出:true

输入:[6,5,4,4]

输出:true

输入:[1,3,2]

输出:false

输入:[1,2,4,5]

输出:true

输入:[1,1,1]

输出:true

注意

  • 1 <= A.length <= 50000

  • -100000 <= A [i] <= 100000

02 第一种解法

使用两个循环,一个判断是否递增,一个判断是否递减,只要其中一个符合条件即可返回true。

public boolean isMonotonic(int[] A) {
return isIncrease(A) || isDecrease(A);
} public boolean isIncrease(int[] A) {
for (int i=0; i<A.length-1; i++) {
if (A[i] > A[i+1]) {
return false;
}
}
return true;
} public boolean isDecrease(int[] A) {
for (int i=0; i<A.length-1; i++) {
if (A[i] < A[i+1]) {
return false;
}
}
return true;
}

03 第二种解法

也可以只是用一次循环。

public boolean isMonotonic2(int[] A) {
boolean isincrease = true;
boolean isdecrease = true;
int n = A.length;
for (int i=0; i<n-1; i++) {
isincrease = isincrease && A[i] <= A[i+1];
isdecrease = isdecrease && A[i] >= A[i+1];
}
return isincrease || isdecrease;
}

04 第三种解法

针对第二种解法,我们分两种情况分别处理了isincrease、isdecrease两个变量。

public boolean isMonotonic3(int[] A) {
boolean isincrease = true;
boolean isdecrease = true;
int n = A.length;
for (int i=0; i<n-1; i++) {
if (A[i] < A[i+1]) {
isdecrease = false;
}
if (A[i] > A[i+1]) {
isincrease = false;
}
}
return isincrease || isdecrease;
}

05 第四种解法

也可以借助包装类Integercompare方法,比较相邻两个元素的大小,另外使用一个变量存储上一次比较的结果,每次做完新的比较后,用新的结果和前一次的结果比较,只要前后两次比较结果不同,可以直接返回false。

public boolean isMonotonic4(int[] A) {
int flag = 0, n = A.length;
for (int i=0; i<n-1; i++) {
int num = Integer.compare(A[i], A[i+1]);
if (num != 0) {
if (flag != 0 && flag != num) {
return false;
}
flag = num;
}
}
return true;
}

06 小结

算法专题目前已连续日更超过六个月,算法题文章213+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.896-单调数组(Monotonic Array)的更多相关文章

  1. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

  2. [Swift]LeetCode896. 单调数列 | Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  3. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  4. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  5. 896. Monotonic Array@python

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  6. C#LeetCode刷题之#896-单调数列(Monotonic Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3760 访问. 如果数组是单调递增或单调递减的,那么它是单调的. ...

  7. 【LeetCode题解】数组Array

    1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...

  8. 【Leetcode_easy】896. Monotonic Array

    problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...

  9. Leetcode896.Monotonic Array单调数列

    如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> = ...

随机推荐

  1. 《Deep Learning》全书已完稿_附全书电子版

    Deep Learning第一篇书籍最终问世了.站点链接: http://www.deeplearningbook.org/ Bengio大神的<Deep Learning>全书电子版在百 ...

  2. mybatis一对多

    mapper.xml <mapper namespace="com.oracle.dao.one2manyDao"> <resultMap type=" ...

  3. Umbrella Header for Module Bolts does not include header &#39;XXXXXX.h&#39;?

    在我们引入第三方Framwork时.有时会出现如标题的警告提示? 怎样解决? Framework 将在下面文件夹下创建一个Module/,并创建一个module.modulemap文件 waterma ...

  4. 关于Text Kit 一些事

    1. Text Kit 是什么? 在iOS7中,苹果引入了Text Kit--Text Kit是一个高速而又现代化的文字排版和渲染引擎.Text Kit在UIKit framework中的定义了一些类 ...

  5. iOS面试常见题

    1.耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13个人围坐一圈,从第一个人開始循环报数,数到三排除,最后剩下的人就是叛徒 int people[13] = {1,2,3, ...

  6. c# 连接各种数据库 Access、Server等

    1.C#连接连接Access程序代码: using System.Data;using System.Data.OleDb;..string strConnection="Provider= ...

  7. windows下安装RubbitMq

    1.下载 下载 rabbitMQ :http://www.rabbitmq.com/download.html,安装rabbitmq需要erlang,下载erlang:http://www.erlan ...

  8. Kills all phantomjs instances, disregard of their origin python关闭进程

    Python/Linux quit() does not terminate PhantomJS process · Issue #767 · SeleniumHQ/selenium https:// ...

  9. mongodb与sql聚合对应图 M

    mongodb与sql聚合对应图 M - CSDN博客 http://blog.csdn.net/u011930016/article/details/49422425 SQL Terms, Func ...

  10. java -jar 与nohup的区别

    ——作为java程序员,经常会遇到这样一个问题,打个jar包,测试或者上线生产,于是乎面临的选择来了,java –jar or nohup? 下面我来扒一扒: 一.    java -jar a.ja ...