```
int countDigitOne(int n) {
if(n <= 0) return 0;
int l = n / 10;
int i = n % 10;
int r = 0;
int power = 1;
int result = 0;
while(i||l)
{
if (l) result += l * power;
if(i)
if(i > 1) result += power;
else result += r + 1;
r += i * power;
i = l % 10;
l = l / 10;
power*=10;
}
return result;
}
```