力扣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
        
相关推荐
地平线开发者8 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
用户7783366132119 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
学习星球12 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
65岁退休Coder13 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文16 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes16 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明16 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn16 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困16 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao18 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode