题目描述:
给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。
示例:
输入: 13
输出: 6
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。
方法1:
主要思路:
(1)每太理解;
class Solution {
public:
int countDigitOne(int n) {
long long res=0;
long long a=1,b=1;
while(n){
res+=(n+8)/10*a+(n%10==1)*b;
b+=n%10*a;
a*=10;
n/=10;
}
return res;
}
};