题解 |从1到n整数中1出现的次数#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
class Solution { public: int NumberOf1Between1AndN_Solution(int n) { long base=1; long res=0; while(base<=n){ long cur=(n/base)%10; long low=n%base; long high=n/base/10; if (cur>1) { res+=(high+1)*base; }else if(cur==1){ res+=(high*base)+(1+low); }else { res+=high*base; } base=base*10; } return (int)res; } };