力扣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
        
相关推荐
SiliconGazer1 天前
第15届国赛满分代码解析(下)—— 运动轨迹算法、按键交互与完整状态机
算法·状态机·stc15f2k60s2·浮点运算·蓝桥杯国赛·运动轨迹、·向量分解
Navigator_Z1 天前
LeetCode //C - 1096. Brace Expansion II
c语言·算法·leetcode
luj_17681 天前
FreeDOS vs MS-DOS PC-DOS 对比解析
服务器·c语言·开发语言·经验分享·算法
笨笨没好名字1 天前
Leetcode刷题python版第一周
python·算法·leetcode
Cthy_hy1 天前
斯特林数:组合划分的递归经典,一二两类全解
python·算法·斯特林数
青春:一叶知秋1 天前
【Python】python基本语法和使用
开发语言·python
不忘不弃1 天前
计算pi的近似值
算法
码云骑士1 天前
12-GIL不是性能杀手(下)-绕过GIL的三种方案与决策树
算法·决策树·机器学习
一只齐刘海的猫1 天前
【Leetcode】无重复字符的最长子串
算法·leetcode·职场和发展
SilentSamsara1 天前
向量数据库实战:Chroma/Milvus/Qdrant 选型与语义搜索应用
开发语言·数据库·人工智能·python·青少年编程·milvus