具体思想:
之前做过一次,逐个位数计算;
具体代码:
using ll=long long;
class Solution {
public:
int countDigitOne(int n) {
ll mod=1;
int cnt=0;
while(mod<=ll(n)){
mod*=10;
cnt++;
}
mod/=10;
int ret=0;
int front=0;
while(cnt>0){
int num=n/mod;
ret+=front*mod;
if(num>=2){
ret+=mod;
}else if(num==1){
ret+=n%mod+1;
}
front=front*10+num;
n=n%mod;
mod/=10;
cnt--;
}
return ret;
}
};```