```
public class Solution {
public int countDigitOne(int n) {
int dbase=1;
int pbase=0;
int track=n;
int res=0;
if(n<=0){
return res;
}
while(track!=0){
int mod=track%10;
if(mod==1){
res+=n%dbase+1+pbase;
}else if(mod>1){
res+=mod*pbase+dbase;
}
pbase=pbase*10+dbase;
dbase*=10;
track/=10;
}
return res;
}
}
```