The idea is to calculate occurrence of 1 on every digit. There are 3 scenarios, for example

```
if n = xyzdabc
```

and we are considering the occurrence of one on thousand, it should be:

```
(1) xyz * 1000 if d == 0
(2) xyz * 1000 + abc + 1 if d == 1
(3) xyz * 1000 + 1000 if d > 1
```

iterate through all digits and sum them all will give the final answer

**Java**

```
public int countDigitOne(int n) {
if (n <= 0) return 0;
int q = n, x = 1, ans = 0;
do {
int digit = q % 10;
q /= 10;
ans += q * x;
if (digit == 1) ans += n % x + 1;
if (digit > 1) ans += x;
x *= 10;
} while (q > 0);
return ans;
}
// 40 / 40 test cases passed.
// Status: Accepted
// Runtime: 0 ms
```

**Python**

```
def countDigitOne(self, n):
if n <= 0:
return 0
q, x, ans = n, 1, 0
while q > 0:
digit = q % 10
q /= 10
ans += q * x
if digit == 1:
ans += n % x + 1
elif digit > 1:
ans += x
x *= 10
return ans
# 40 / 40 test cases passed.
# Status: Accepted
# Runtime: 32 ms
# 97.59%
```