题解 | #从1到n整数中1出现的次数#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
class Solution {
public:
int NumberOf1Between1AndN_Solution(int n) {
int ans=0,t=1;
int m=n;
while(n!=0){
ans+=((n-1)/10+1)*t;
if((m/t)%10==1){
ans-=(2*t-m%(t*10)-1);
}
t*=10;
n/=10;
}
return ans;
}
};


正浩创新EcoFlow公司福利 646人发布