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

An array A is monotone increasing if for all i <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[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. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000

这道题让我们判断一个数组是否单调,单调数组就是说这个数组的数字要么是递增的,要么是递减的,不存在一会儿递增一会儿递减的情况,即不会有山峰存在。这里不是严格的递增或递减,是允许有相同的数字的。那么我们直接将相邻的两个数字比较一下即可,使用两个标识符,inc 和 dec,初始化均为 true,我们开始时假设这个数组既是递增的又是递减的,当然这是不可能的,我们会在后面对其进行更新。在遍历数组的时候,只要发现某个数字大于其身后的数字了,那么 inc 就会赋值为 false,同理,只要某个数字小于其身后的数字了,dec 就会被赋值为 false,所以在既有递增又有递减的数组中,inc 和 dec 都会变为 false,而在单调数组中二者之间至少有一个还会保持为 true,参见代码如下:


解法一:

class Solution {
public:
bool isMonotonic(vector<int>& A) {
bool inc = true, dec = true;
for (int i = 1; i < A.size(); ++i) {
inc &= (A[i - 1] <= A[i]);
dec &= (A[i - 1] >= A[i]);
if (!inc && !dec) return false;
}
return true;
}
};

跟上面的解法思路很像,只不过没有用 bool 型的,而是用了整型数字来记录递增和递减的个数,若是单调数组,那么最终在 inc 和 dec 中一定会有一个值是等于数组长度的,参见代码如下:


解法二:

class Solution {
public:
bool isMonotonic(vector<int>& A) {
int inc = 1, dec = 1, n = A.size();
for (int i = 1; i < n; ++i) {
inc += (A[i - 1] <= A[i]);
dec += (A[i - 1] >= A[i]);
}
return (inc == n) || (dec == n);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/896

参考资料:

https://leetcode.com/problems/monotonic-array/

https://leetcode.com/problems/monotonic-array/discuss/165889/C%2B%2BJavaPython-One-Pass-O(N)

https://leetcode.com/problems/monotonic-array/discuss/172578/Java-O(n)-simple-solution

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 896. Monotonic Array 单调数组的更多相关文章

  1. 896. Monotonic Array单调数组

    [抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...

  2. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

  3. LeetCode 896. Monotonic Array

    原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...

  4. 896. Monotonic Array@python

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

  5. 【Leetcode_easy】896. Monotonic Array

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

  6. Leetcode896.Monotonic Array单调数列

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

  7. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [LeetCode&Python] Problem 896. Monotonic Array

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

  9. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

随机推荐

  1. git使用cherry-pick和revert抢救错误代码提交

    大多数的新手在新接触git时都会出现这样的问题.代码写完了,提交到dev分支进行测试.一高兴忘记切回来,继续在dev分支开发,写完之后提交时猛的发现,我靠,我怎么在dev上面写代码,此时内心必然是一阵 ...

  2. Vue.js 源码分析(三十二) 总结

    第一次写博客,坚持了一个多月时间,Vue源码分析基本分析完了,回过头也看也漏了一些地方,比如双向绑定里的观察者模式,也可以说是订阅者模式,也就是Vue里的Dep.Watcher等这些函数的作用,网上搜 ...

  3. python threading ThreadPoolExecutor

    线程池,为什么要使用线程池:1. 线程中可以获取某一个线程的状态或者某一个任务的状态,以及返回值2. 当一个线程完成的时候我们主线程能立即知道3. futures可以让多线程和多进程编码接口一致 获取 ...

  4. C# 中如何深度复制某一个类型(备注:可能有 N 个类型需要复制)的对象?

    如题,针对某一个特定的类型,深度复制,没有那么难,最起码可以手动赋值,但如果要针对 N 多类型深度复制,最简单的方法,是把这个对象序列化成 XML.JSON 或其它可以序列化的载体,然后再将这个载体反 ...

  5. [基础] - 从xx语言是跨平台的说起

    我经常碰到一些人在说xx语言跨平台而yy语言不是(为避免不必要的纷争,在此不写具体语言但不影响阅读),从而来表明自己使用xx语言进行程序开发进而在编程语言鄙视链上高高在上很有优越感. 大概是从Java ...

  6. JPA技术之EntityManager使用方法

    Session bean or MD bean对Entity bean的操作(包括所有的query, insert, update, delete操作)都是通过EntityManager实例来完成的. ...

  7. long类型在内存中占8个字节,float类型在内存中占4个字节,为什么long还要比float小呢?

    结论:数值范围大小和占用的字节没有关系. float类型的范围: 负数:-3.402823E38~-1.401298E-45 整数:0 正数:1.401298E-45~3.402823E38 long ...

  8. itextpdf确定页面坐标的方式

    itextpdf的确定页面上的具体坐标是通过坐标来确定的. 它们的坐标轴是这样的:以左下角为原点的xy坐标轴. 在itextpdf的方法中中,定义某个点的位置,都是以左下方为原点坐标轴来确定. 如果要 ...

  9. Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip

    目录 Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip 场景 过程还原 结论 参考资料 Nginx+Docker部署模式下 asp.net core 获取真实的客户端 ...

  10. Centos7 下安装Redis4.0.6

    一.安装redis 第一步:下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz [root@iZwz991stxd ...