LeetCode(力扣)738. 单调递增的数字Python

LeetCode738. 单调递增的数字

题目链接

https://leetcode.cn/problems/monotone-increasing-digits/description/

代码

python 复制代码
class Solution:
    def monotoneIncreasingDigits(self, n: int) -> int:
        strNum = str(n)
        flag = len(strNum)

        for i in range(len(strNum) - 1, 0, -1):
            if strNum[i] < strNum[i - 1]:
                flag = i
                strNum = strNum[:i - 1] + str(int(strNum[i - 1]) - 1) + strNum[i:]

        for i in range(flag, len(strNum)):
            strNum = strNum[:i] + '9' + strNum[i + 1:]

        return int(strNum)
相关推荐
2401_8812444024 分钟前
P3808 AC 自动机(简单版)
算法
dlraba8021 小时前
用 Python+OpenCV 实现实时文档扫描:从摄像头捕捉到透视矫正全流程
开发语言·python·opencv
Jayden_Ruan1 小时前
C++十进制转二进制
数据结构·c++·算法
小熊出擊1 小时前
【pytest】fixture 内省(Introspection)测试上下文
python·单元测试·pytest
Haooog1 小时前
98.验证二叉搜索树(二叉树算法题)
java·数据结构·算法·leetcode·二叉树
njsgcs1 小时前
sse mcp flask 开放mcp服务到内网
后端·python·flask·sse·mcp
一人の梅雨1 小时前
1688 店铺商品全量采集与智能分析:从接口调用到供应链数据挖掘
开发语言·python·php
Terio_my2 小时前
Python制作12306查票工具:从零构建铁路购票信息查询系统
开发语言·python·microsoft
Macre Aegir Thrym2 小时前
MINIST——SVM
算法·机器学习·支持向量机
万粉变现经纪人2 小时前
如何解决 pip install -r requirements.txt 约束文件 constraints.txt 仅允许固定版本(未锁定报错)问题
开发语言·python·r语言·django·beautifulsoup·pandas·pip