[力扣] 算法 896 (C#)

896. 单调数列

public class Solution {
    public bool IsMonotonic(int[] A) {
        if (A.Length > 1)
        {
            var cmp = 0;
            for (var i = 1; i < A.Length; i++)
            {
                var nextCmp = A[i - 1].CompareTo(A[i]);
                if ((cmp ^ nextCmp) == -2)
                    return false;
                cmp = cmp == 0 ? nextCmp : cmp;
            }
        }
        return true;
    }
}

发表评论