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

LeetCode738. 单调递增的数字

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/monotone-increasing-digits/description/
LeetCode(力扣)738. 单调递增的数字Python_第1张图片

代码

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)

你可能感兴趣的:(leetcode,python,算法,职场和发展)