分类讨论: 讨论每位上1出现的次数
// 1~n中 1出现的次数
class Solution {
public:
int numberOf1Between1AndN_Solution(int n) {
if(!n) return 0;
vector<int> number, prefix;
while(n) number.push_back(n%10),prefix.push_back(n),n/=10;
int left=0;
int res=0;
for(int i=number.size()-1;i>=0;i--) {
int t=0, right=0;
t=pow(10, i);
right=prefix[0]-prefix[i]*t;
res += left*t;
if(number[i]==1) res += right+1;
else if(number[i]>1) res +=t;
left = left*10 + number[i];
}
return res;
}
};