这是悦乐书的第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. C#语法复习3

    第七章 类与继承 1.虽然派生类不能删除基类的的任何成员,但我们可以利用在派生类当中声明与基类成员名称相同的成员来屏蔽基类成员.这叫 覆盖. 一种是隐式屏蔽.一种是显式屏蔽.所谓 显式就是 加上一个n ...

  2. GCD编程(封装GCD)

    //GCDGroup 类 @interface GCDGroup : NSObject @property (strong, nonatomic, readonly) dispatch_group_t ...

  3. PHP中include路径修改

    1.__FILE__ __FILE__ always equals to the real path of a php script regardless whether it's included. ...

  4. CentOS笔记-其他杂记

    1.忘记密码时,可以用single模式来修改密码,为了安全,可以禁用single模式,参考网址如下 Centos服务器禁止单用户模式(single)来增强系统安全 2.远程登录:ssh root@xx ...

  5. Memory cycles about Block

    Block keeps a strong point to all object referenced in side of them, so all object will stay in heap ...

  6. POJ1459 Power Network —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1459 Power Network Time Limit: 2000MS   Memory Limit: 32768K Tot ...

  7. DEDE内容页调用栏目的SEO标题、描述、关键字的方法

    上篇写了<dedecms栏目页调用栏目关键词.描述的方法>,本章雨田SEOER讲述DEDE内容页调用栏目的SEO标题.描述.关键字的方法内容页调用SEO标题:在<title>& ...

  8. WebAssembly,可以作为任何编程语言的编译目标,使应用程序可以运行在浏览器或其它代理中——浏览器里运行其他语言的程序?

    Mozilla.谷歌.微软和苹果已经决定开发一种面向Web的二进制格式.该格式名为WebAssembly,可以作为任何编程语言的编译目标,使应用程序可以运行在浏览器或其它代理中. 几年前,我们在Inf ...

  9. POJ3304:Segments (几何:求一条直线与已知线段都有交点)

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  10. NSArray是强引用容器

    经常比较疑惑NSArray.NSDictionary.NSSet这几个对象容器管理对象所采用的方式是“强引用”还是“弱引用”. 通过简单的命令行程序得到的结论是“NSArray.NSDictionar ...