My recursive simple python solution


  • -1
    S
    class Solution(object):
    def countDigitOne(self, n):
        if n <= 0:
            return 0
        count = 0
        m = 1
        while m * 10 <= n:
            m *= 10
    
        count += min(2 * m - 1, n) - m + 1
        count +=  (n // m) * self.countDigitOne(m - 1) + self.countDigitOne(n % m)
    
        return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.