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)
相关推荐
墨绿色的摆渡人9 分钟前
pytorch小记(二十二):全面解读 PyTorch 的 `torch.cumprod`——累积乘积详解与实战示例
人工智能·pytorch·python
Dr.92713 分钟前
1-10 目录树
java·数据结构·算法
小白—人工智能24 分钟前
数据分析 —— 数据预处理
python·数据挖掘·数据分析
子豪-中国机器人25 分钟前
C++ 蓝桥 STEMA 省选拔赛模拟测试题(第一套)
开发语言·c++·算法
若叶时代26 分钟前
数据分析_Python
人工智能·python·数据分析
callJJ27 分钟前
Bellman - Ford 算法与 SPFA 算法求解最短路径问题 ——从零开始的图论讲解(4)
数据结构·算法·蓝桥杯·图论·单源最短路径·bellman- ford算法
圈圈编码30 分钟前
LeetCode Hot100刷题——轮转数组
java·算法·leetcode·职场和发展
英英_36 分钟前
python 爬虫框架介绍
开发语言·爬虫·python
大模型铲屎官2 小时前
【Python-Day 14】玩转Python字典(上篇):从零开始学习创建、访问与操作
开发语言·人工智能·pytorch·python·深度学习·大模型·字典
yunvwugua__2 小时前
Python训练营打卡 Day27
开发语言·python