class Solution {
public:
/*
int find_num1(int n){
int count = 0;
while(n){
if(n%10 == 1){
count++;
}
n /= 10;
}
return count;
}

//1-n中 1 出现的次数
int NumberOf1Between1AndN_Solution(int n) {
    int cnt = 0;
    for(int i = 1;i <= n;i++){
        cnt += find_num1(i);
    }
    return cnt;
}
*/
int NumberOf1Between1AndN_Solution(int n) {
    if(n <= 0) return 0;
    vector<int> v;
    int ret = 0;
    while(n){
        v.push_back(n%10);
        n /= 10;
    }
    for(int i = v.size()-1;i >= 0;--i){
        auto left = 0,right = 0,t = 1;

        for(int j = v.size() - 1;j > i;--j){
            left = left * 10 + v[j];
        }
        for(int j = i-1;j >= 0;--j){
            right = right * 10 + v[j];
            t *= 10;
        }
        ret = ret + left*t;
        if(v[i] == 1){
            ret += (right+1);
        }else if (v[i] > 1)
            ret += t;
    }
    return ret;
}

};