```
public int countDigitOne(int n) {
if (n<=0) return 0;
int multiplier = 1;
while (multiplier <= n/10) multiplier *= 10;
if (multiplier == 1) return 1;
int mod = n%multiplier;
int div = n/multiplier;
int add = div == 1 ? mod + 1 : multiplier;
return add + div*countDigitOne(multiplier-1) + countDigitOne(mod);
}
```