力扣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
        
相关推荐
weixin_452159554 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
Hello.Reader6 分钟前
面向 403 与域名频繁变更的合规爬虫工程实践以 Libvio 系站点为例
爬虫·python·网络爬虫
独自破碎E9 分钟前
【总和拆分 + 双变量遍历】LCR_012_寻找数组的中心下标
数据结构·算法
WBluuue10 分钟前
Codeforces 1076 Div3(ABCDEFG)
c++·算法
深蓝海拓19 分钟前
PySide6从0开始学习的笔记(二十五) Qt窗口对象的生命周期和及时销毁
笔记·python·qt·学习·pyqt
u01092727121 分钟前
模板编译期排序算法
开发语言·c++·算法
Dfreedom.29 分钟前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
GIS瞧葩菜30 分钟前
Cesium 轴拖拽 + 旋转圈拖拽 核心数学知识
人工智能·算法·机器学习
2301_7903009633 分钟前
用Python读取和处理NASA公开API数据
jvm·数据库·python
m0_6860416137 分钟前
C++中的适配器模式变体
开发语言·c++·算法