LeetCode738. 单调递增的数字
- 题目链接
- 代码
题目链接
https://leetcode.cn/problems/monotone-increasing-digits/description/
代码
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)