Simple python solution


  • 0
    Y
    class Solution(object):
        def countDigitOne(self, n):
            d = 1
            count = 0
            range_total = 0   # number of 1's in [0, d / 10]
            while d <= n:
                r = n % d
                c = (n % (10 * d)) / d
    
                if c > 0:
                    count = c * range_total + count + (d if c > 1 else (r + 1))
    
                range_total = 10 * range_total + d
                d *= 10
    
            return count

Log in to reply
 

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