單調遞增的數字 循環迭代

原文鏈接:https://leetcode.com/problems/monotone-increasing-digits/discuss/181945/Fast-and-simple-40ms-Python-solution-using-recursion

 

class Solution:
    def monotoneIncreasingDigits(self, N: int) -> int:
        s = str(N)
        l = len(s)
        res = 0
        for i in range(len(s)):
            if i == 0 or s[i] >= s[i-1]:
                res += int(s[i]) * pow(10, l-1)
            else:
                return self.monotoneIncreasingDigits(res-1)
            l -= 1
        return res

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章