这题看着简单,但是这个算每一位1个数的公式还是没有很推明白
再慢慢想想吧
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;
n /= 10;
high = n/10;
cur = n%10;
digit *= 10;
}
return res;
}
}