力扣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
        
相关推荐
江畔柳前堤3 分钟前
大语言模型分布式训练:从并行策略到万卡工程的系统梳理
人工智能·分布式·深度学习·算法·目标检测·机器学习·语言模型
Forever Nore1 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
北斗落凡尘2 小时前
LangGraph 入门实战(2)
python·langchain
ttod_qzstudio3 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
罗西的思考3 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX4 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm
jufeng13074 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 1 篇】
人工智能·python·架构·agent
爱跳舞的烤冷面4 小时前
自学嵌入式第22天(数据结构——哈希)
数据结构·算法·哈希算法
猎嘤一号4 小时前
博弈论(Game Theory)的理论、算法与工程
人工智能·算法·安全·博弈论
月光船幽幽5 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法