class Solution {
public:
int countDigitOne(int n) {
if(n < 0)
{
return 0;
}
int left;
int right = 0;
int index = 0;
int cur = 0;
int res = 0;
while(n)
{
cur = n % 10;
left = n / 10;
if(cur == 0)
{
res += left * pow(10, index);
}
else if(cur == 1)
{
res += left * pow(10, index) + right + 1;
}
else
{
res += (left + 1) * pow(10, index);
}
right += cur * pow(10, index);
index++;
n /= 10;
}
return res;
}
};