力扣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
        
相关推荐
幼稚园的山代王29 分钟前
python3基础语法梳理(一)
开发语言·python
Gyoku Mint42 分钟前
机器学习×第五卷:线性回归入门——她不再模仿,而开始试着理解你
人工智能·python·算法·机器学习·pycharm·回归·线性回归
Blossom.1181 小时前
基于机器学习的智能故障预测系统:构建与优化
人工智能·python·深度学习·神经网络·机器学习·分类·tensorflow
华科云商xiao徐1 小时前
Python多线程数据爬取程序模版
爬虫·python
大白爱琴1 小时前
使用python进行图像处理—像素级操作与图像算术(4)
开发语言·图像处理·python
吴声子夜歌1 小时前
OpenCV——图像基本操作(一)
python·opencv·计算机视觉
zhanghongyi_cpp1 小时前
美食出处(文件版)
python
蒙奇D索大1 小时前
【数据结构】图论最短路径算法深度解析:从BFS基础到全算法综述
数据结构·算法·图论·广度优先·图搜索算法
trouvaille1 小时前
哈希数据结构的增强
算法·go
我不是小upper2 小时前
L1和L2核心区别 !!--part 2
人工智能·深度学习·算法·机器学习