```
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
```