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. rapoo mt700键盘mac osx不能复制问题

    问题描述:rapoo mt700键盘mac osx,按windows建+c不能复制,其它按键正常 解决办法:检查右上角windows等是否亮,如果是亮着按FN+WIN 切换模式 操作方法: 有线模式: ...

  2. CSS3实现文字描边的2种方法

    问题 最近遇到一个需求,需要实现文字的描边效果,如下图           解决方法一     首先想到去看CSS3有没有什么属性可以实现,后来被我找到了text-stroke     该属性是一个复 ...

  3. conda基本知识

    卸载anaconda: rm -rf anaconda3 (anaconda文件夹名称) conda删除虚拟环境 在终端执行:conda remove -n your_env_name(虚拟环境名称) ...

  4. 用 qemu-user 在arm linux机器上运行amd64/x86程序

    1. qemu-user 是什么 本来, 对于 QEmu, 我只知道它是一个模拟器, 可以像 VirtualBox/VMWare 那样跑一个操作系统, 只不过 QEmu 可以在 AMD64 上面跑针对 ...

  5. Knative 基本功能深入剖析:Knative Serving 自动扩缩容 Autoscaler

    Knative Serving 默认情况下,提供了开箱即用的快速.基于请求的自动扩缩容功能 - Knative Pod Autoscaler(KPA).下面带你体验如何在 Knative 中玩转 Au ...

  6. 『珂朵莉树 Old Driver Tree』

    珂朵莉树 珂朵莉树其实不是树,只是一个借助平衡树实现的数据结构,主要是对于有区间赋值的数据结构题,可以用很暴力的代码很高效地完成任务,当然这是建立在数据随机的基础上的. 即使数据不是随机的,写一个珂朵 ...

  7. PIE SDK水体指数法

    1.算法功能简介 单波段阈值法是通过选择某单一波段为判识参数,这一波段往往是水体特征最明显而其它地物相对不太突出的波段(如近红外波段和中红外波段),然后再划定阈值来确定水体信息.该方法主要是利用水体在 ...

  8. Python笔记:threading(多线程操作)

    Python的线程操作在旧版本中使用的是thread模块,在Python27和Python3中引入了threading模块,同时thread模块在Python3中改名为_thread模块,thread ...

  9. JS中使用RSA加密信息

    加密重要信息,如用户名.密码.防止http拦截.浏览器使用公钥加密,服务器端使用私钥解密 页面添加引用:   jsencrypt.min.js // 3-Url参数加密类 if (window.JSE ...

  10. Java foreach循环

    foreach循环:增强性的for循环应用: 在for语句中,需要使用索引来进行操作具体的数组或集合内容操作:而foreach可以取消索引的操作细节: for ( 类型 变量 : 数组 | 集合 ) ...