**Solution**

**Number of Digit One** https://leetcode.com/problems/number-of-digit-one/?tab=Description

**Algorithm**

- https://leetcode.com/discuss/44314/accepted-solution-using-counting-principle-with-explanation
- Concept: Try to replace every digit by 1 and count the combinations possible. The existing digit may be 1, 0 or greater than 1.

```
class Solution(object):
def countDigitOne(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 0:
return 0
factor, count, highN = 1, 0, n
while highN:
# Assume number was 240312
highN, lowN, lowC = n//(factor*10), int(n%factor), factor
d = int(n//factor % 10)
factor = factor*10
if d == 1:
count = count + (lowN+1) + (highN*lowC)
elif d == 0:
count = count + (highN*lowC)
else:
count = count + (1+highN)*(lowC)
return count
```