LeetCode Contest 100 单调数列

版权声明:欢迎提问:[email protected] https://blog.csdn.net/include_heqile/article/details/82312966

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

我的解决方案:

package tet;

class Solution
{
    public boolean isMonotonic(int[] A)
    {
        //假设为单调递减
        boolean flagDec=true;
        for(int i=0; i<A.length-1; i++)
        {
            if(A[i]<A[i+1])
            {
                flagDec=false;
                break;
            }
        } 
        if(flagDec) return flagDec;

        //再判断是否为单调递增
        boolean flagInc=true;
        for(int i=0; i<A.length-1; i++)
        {
            if(A[i]>A[i+1])
            {
                flagInc=false;
                break;
            }
        }
        if(flagInc)return true;

        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/include_heqile/article/details/82312966