可能我的数学太垃圾了。。。 给个大佬的链接大佬的题解 就两个字 牛皮 膜拜 按着大佬的思路写了个c++的 顺便 weight 要是double的。。。 要不然会越界 (至于 double*int 忽略吧 虽然说这么写不太好。。。。)
class Solution { public: int countDigitOne(int n) { double weight=1; int res=0; int high=n/10,cur=n%10,low=0; while(high!=0||cur!=0){ if(cur==0){ res+=high*weight; }else if(cur==1){ res+=high*weight+low+1; }else{ res+=(high+1)*weight; } low+=weight*cur; weight*=10; cur=high%10; high/=10; } return res; } };