Number of Digit One

Problem Id: 233 Difficulty: Hard


Intuition

Solution


class Solution:
    def countDigitOne(self, n: int) -> int:
        if n < 0:
            return 0
        s = str(n)
        count = 0
        l = len(str(n))
        for i in range(1, l+1):
            num = int(s[-i])
            count += (n // (10 ** i) + 1) * (10 ** (i-1))
            if num <= 1:
                count -= 10 ** (i-1)
            if num == 1:
                count += n % (10 ** (i-1)) + 1

        return count