Loading

896. 单调数列

896. 单调数列

https://leetcode-cn.com/contest/weekly-contest-100/problems/monotonic-array/

package com.test;

/**
 * @author stono
 * @date 2018/9/2
 */
public class Lesson896 {
    public static void main(String[] args) {
        int[] A = {1,2,2,3};
        boolean monotonic = isMonotonic(A);
        System.out.println(monotonic);

    }

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

 

posted @ 2018-09-02 09:40  stono  阅读(240)  评论(0编辑  收藏  举报