力扣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
        
相关推荐
老鼠只爱大米4 分钟前
LeetCode经典算法面试题 #104:二叉树的最大深度(深度优先搜索、广度优先搜索等多种实现方案详细解析)
算法·leetcode·二叉树·dfs·bfs·深度优先搜索·广度优先搜索
疯狂的喵5 分钟前
分布式系统监控工具
开发语言·c++·算法
爱尔兰极光7 分钟前
LeetCode热题100--两数之和
算法·leetcode·职场和发展
2301_8223827610 分钟前
模板编译期排序算法
开发语言·c++·算法
m0_5613596715 分钟前
嵌入式C++调试技术
开发语言·c++·算法
yuan1999719 分钟前
高光谱遥感图像异常检测KRX算法Matlab实现
算法·机器学习·matlab
努力学习的小廉20 分钟前
我爱学算法之—— 回溯
算法·深度优先
2301_7634724620 分钟前
C++中的享元模式高级应用
开发语言·c++·算法
hcnaisd221 分钟前
使用Python进行PDF文件的处理与操作
jvm·数据库·python