代码随想录算法训练营第37天|738.单调递增的数字 968.监控二叉树

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.监控二叉树 (可以跳过)

跳过

你可能感兴趣的:(算法,leetcode,c++)