力扣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
        
相关推荐
programhelp_17 分钟前
特斯拉 MLE 超详细面经 + 避坑
数据结构·人工智能·算法·面试·职场和发展
程序员敲代码吗38 分钟前
提升Python编程效率的五大特性
开发语言·python
越甲八千38 分钟前
深入了解迭代器erase()之后的失效逻辑
算法
躺柒40 分钟前
读人工智能全球格局:未来趋势与中国位势06人类的未来(下)
大数据·人工智能·算法·ai·智能
List<String> error_P1 小时前
Python蓝桥杯常考知识点-模拟
开发语言·python·蓝桥杯
L_Aria1 小时前
6421. 【NOIP2019模拟11.11】匹配
c++·算法·动态规划
比奇堡鱼贩1 小时前
python第五次作业
开发语言·前端·python
骇城迷影2 小时前
代码随想录:哈希表篇
算法·哈希算法·散列表
智者知已应修善业2 小时前
【PAT乙级真题解惑1012数字分类】2025-3-29
c语言·c++·经验分享·笔记·算法
码农小韩2 小时前
AIAgent应用开发——DeepSeek分析(二)
人工智能·python·深度学习·agent·强化学习·deepseek