2020-07-30
1.题目描述
输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。
2.题解
找规律
3.代码
class Solution {
public:
int countDigitOne(int n) {
int high=n/10,low=0,cur=n%10,res=0;
long digit=1;
while (low!=n){
if (cur==0) res+=high*digit;
else if (cur==1) res+=high*digit+low+1;
else res+=(high+1)*digit;
int t=cur;
cur=high%10;
high=high/10;
low=t*digit+low;
digit*=10;
}
return res;
}
};