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;
    }
};