遇到难题就要面对复制粘贴编程。嘿嘿嘿。《剑指offer》上面的解题方法太复杂。Krahets大佬的题解就要简单多了,思考,复制粘贴,跑路。一气呵成。背下来就完事了。题解链接
class Solution {
public int countDigitOne(int n) {
int cur = n % 10;
int high = n / 10;
int low = 0;
int res = 0;
int digit = 1;
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;
}
}