738.单调递增的数字
class Solution(object):
def monotoneIncreasingDigits(self, n):
"""
:type n: int
:rtype: int
"""
#set a to a list of string
a = list(str(n))
#loop from the last digit
for i in range(len(a)-1,0,-1):
#if the cur number is smaller than its previous number
if int(a[i]) < int(a[i-1]):
#we set up the previous number minus 1
a[i-1] = str(int(a[i-1]) - 1)
#renew all number to 9 behind cur including cur
a[i:] = '9' * (len(a) - i)
#remember set it as int
return int("".join(a))
968.监控二叉树 (可以跳过)
跳过
单调递增数字处理与二叉树监控
文章介绍了如何找到一个整数n的单调递增数字表示,当遇到降序时将前一个数字减一并后续转为9。同时提到了一个关于二叉树监控的问题,但提供了跳过选项。

被折叠的 条评论
为什么被折叠?



