题目描述:
思路:可以从左向右每个位得到最终的结果。对于每个当前位,其出现1的次数可以根据当前位数字分为三类,0一类,1一类,2~9一类。(语言表达能力有限。。具体解法请参考官方题解吧)
class Solution {
public int countDigitOne(int n) {
int digit = 1, res = 0;
int high = n / 10, cur = n % 10, low = 0;
while(high != 0 || cur != 0) {
if(cur == 0) res += high * digit;
else if(cur == 1) res += high * digit + low + 1;
else res += (high + 1) * digit;
low += cur * digit;
cur = high % 10;
high /= 10;
digit *= 10;
}
return res;
}
}