• 欢迎光临~

leetcode-896-easy

开发技术 开发技术 2022-10-19 次浏览

Monotonic Array
思路一: 遍历

public boolean isMonotonic(int[] nums) {
    boolean increase = true;
    for (int i = 0; i < nums.length - 1; i++) {
        if (nums[i] > nums[i + 1]) {
            increase = false;
            break;
        }
    }

    boolean decrease = true;
    for (int i = 0; i < nums.length - 1; i++) {
        if (nums[i] < nums[i + 1]) {
            decrease = false;
            break;
        }
    }

    return increase || decrease;
}
程序员灯塔
转载请注明原文链接:leetcode-896-easy
喜欢 (0)