力扣896

python 复制代码
bool isMonotonic(int* nums, int numsSize) {
    if (numsSize <= 1) {
        return true;
    }
    
    bool isIncreasing = true;
    bool isDecreasing = true;
    
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] > nums[i - 1]) {
            isDecreasing = false;
        }
        if (nums[i] < nums[i - 1]) {
            isIncreasing = false;
        }
    }
    
    return isIncreasing || isDecreasing;
}
python 复制代码
class Solution:
    def isMonotonic(self, nums: List[int]) -> bool:
        # 初始化标志变量
        isIncreasing = True
        isDecreasing = True
        
        # 遍历数组
        for i in range(1, len(nums)):
            if nums[i] > nums[i - 1]:
                isDecreasing = False
            if nums[i] < nums[i - 1]:
                isIncreasing = False
        
        # 返回结果
        return isIncreasing or isDecreasing
        
相关推荐
web135085886353 小时前
Python大数据可视化:基于python的电影天堂数据可视化_django+hive
python·信息可视化·django
东方芷兰3 小时前
伯克利 CS61A 课堂笔记 11 —— Mutability
笔记·python
学编程的小程5 小时前
LeetCode216
算法·深度优先
leeyayai_xixihah5 小时前
2.21力扣-回溯组合
算法·leetcode·职场和发展
01_5 小时前
力扣hot100——相交,回文链表
算法·leetcode·链表·双指针
萌の鱼5 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
Buling_05 小时前
算法-哈希表篇08-四数之和
数据结构·算法·散列表
AllowM5 小时前
【LeetCode Hot100】除自身以外数组的乘积|左右乘积列表,Java实现!图解+代码,小白也能秒懂!
java·算法·leetcode
RAN_PAND5 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
不会Hello World的小苗5 小时前
Java——列表(List)
java·python·list