剑指 Offer 43. 1~n 整数中 1 出现的次数
输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。
例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。
示例 1:
输入:n = 12
输出:5
数位DP
直接看路飞大佬的解释就行了Orz
注意一些细节,代码注释里写了
class Solution {
public:
int countDigitOne(int n) {
int res = 0;
int high = n / 10, cur = n % 10, low = 0;
//注意digit会溢出,所以使用long
long digit = 1;
while(high != 0 || cur != 0){
//cout<<high<<" "<<cur<<" "<<low<<endl;
if(cur == 0) res += int(high * digit);
else if(cur == 1) res += int(high * digit + low + 1);
else res += int((high + 1) * digit);
//这4个语句的顺序不要换!
low += int(cur * digit);
cur = high % 10;
high /= 10;
digit *= 10;
}
return res;
}
};