class Solution {
public:
int NumberOf1Between1AndN_Solution(int n)
{
int nCurBit = n % 10;
int nForward = n / 10;
int nBackward = 0;
int nBase = 1;
int nSum = 0;
while(n / nBase){
if(nCurBit == 0)
nSum += nForward * nBase;
else if(nCurBit == 1){
nSum += nForward * nBase;
nSum = nSum + nBackward + 1;
}else
nSum += (nForward + 1) * nBase;
nCurBit = nForward % 10;
nBase *= 10;
nForward = n / (nBase * 10);
nBackward = n % nBase;
}
return nSum;
}
};