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)
相关推荐
墨雪不会编程10 分钟前
数据结构—排序算法篇二
数据结构·算法·排序算法
tianyuanwo14 分钟前
Rust开发完全指南:从入门到与Python高效融合
开发语言·python·rust
ShineWinsu33 分钟前
对于数据结构:堆的超详细保姆级解析—上
数据结构·c++·算法·计算机·二叉树·顺序表·
im_AMBER1 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
如何原谅奋力过但无声1 小时前
TensorFlow 2.x常用函数总结(持续更新)
人工智能·python·tensorflow
程序员-小李1 小时前
基于 Python + OpenCV 的人脸识别系统开发实战
开发语言·python·opencv
努力学算法的蒟蒻1 小时前
day09(11.6)——leetcode面试经典150
算法·leetcode·职场和发展
2301_796512521 小时前
Rust编程学习 - 内存分配机制,如何动态大小类型和 `Sized` trait
学习·算法·rust
万粉变现经纪人1 小时前
如何解决 pip install 安装报错 [WinError 32] 文件被占用(杀毒/占用进程)问题
python·pycharm·flask·beautifulsoup·bug·pandas·pip
java1234_小锋2 小时前
[免费]基于Python的Flask酒店客房管理系统【论文+源码+SQL脚本】
开发语言·人工智能·python·flask·酒店客房