资讯专栏INFORMATION COLUMN

[LeetCode] 896. Monotonic Array

livem / 3185人阅读

Problem

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

Solution
class Solution {
    public boolean isMonotonic(int[] A) {
        if (A.length <= 2) return true;
        int i = 1;
        while (i < A.length && A[i] == A[i-1]) {
            i++;
        }
        if (i == A.length) return true;
        else if (A[i] > A[i-1]) return isInc(A, i);
        else return isDec(A, i);
    }
    private boolean isInc(int[] A, int start) {
        for (int i = start; i < A.length; i++) {
            if (A[i] < A[i-1]) return false;
        }
        return true;
    }
    private boolean isDec(int[] A, int start) {
        for (int i = start; i < A.length; i++) {
            if (A[i] > A[i-1]) return false;
        }
        return true;
    }
}
Two flag
class Solution {
    public boolean isMonotonic(int[] A) {
        int inc = 0, dec = 0;
        for (int i = 1; i < A.length; i++) {
            if (A[i] > A[i-1]) inc = 1;
            if (A[i] < A[i-1]) dec = 1;
        }
        if (inc == 1 && dec == 1) return false;
        return true;
    }
}
Or
class Solution {
    public boolean isMonotonic(int[] A) {
        boolean inc = true, dec = true;
        for (int i = 1; i < A.length; i++) {
            inc &= A[i] >= A[i-1];
            dec &= A[i] <= A[i-1];
        }
        return inc || dec;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77261.html

相关文章

  • Leetcode PHP题解--D66 896. Monotonic Array

    摘要:题目链接题目分析给定一个数字数组,判断是否单调递增或递减。判断后,再逐个遍历。若为单调递减,则不能出现大于前一个数组的值。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D66 896. Monotonic Array 题目链接 896. Monotonic Array 题目分析 给定一个数字数组,判断是否单调递增或递减。 单调递增即,对于第n位数字,其后面的数组都大于或等于它。 ...

    henry14 评论0 收藏0
  • Swoole 4.4:支持 CURL 协程化

    摘要:在之前的版本中,一直不支持协程化,在代码中无法使用。由于使用了库实现,无法直接它的,版本使用模拟实现了的,并在底层替换了等函数的。跟踪使用跟踪发现,所有系统调用均变成的异步非阻塞调用了。 在4.4之前的版本中,Swoole一直不支持CURL协程化,在代码中无法使用curl。由于curl使用了libcurl库实现,无法直接hook它的socket,4.4版本使用SwooleCorouti...

    RobinTang 评论0 收藏0
  • Bytom信息上链教程

    摘要:那如何实现信息上链呢使用特殊的操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。好了,通过以上的个步骤,我们就可以借助比原链实现信息上链。 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 用比原链(Byt...

    Yangder 评论0 收藏0
  • 896-单调数列

    摘要:前言的第一题单调数列,一道送分题,当时时间有限,所以用了最简单的实现方案,原题目如下如果数组是单调递增或单调递减的,那么它是单调的。当给定的数组是单调数组时返回,否则返回。 前言 Weekly Contest 100的第一题单调数列,一道送分题,当时时间有限,所以用了最简单的实现方案,原题目如下: 如果数组是单调递增或单调递减的,那么它是单调的。 如果对于所有 i

    王晗 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<