class Solution {
public:
int numberOf2sInRange(int n) {
if(n < 2)
{
return 0;
}
int left;
int right = 0;
int index = 0;
int cur = 0;
int res = 0;
while(n)
{
cur = n % 10;
left = n / 10;
if(cur < 2)
{
res += left * pow(10, index);
}
else if(cur == 2)
{
res += left * pow(10, index) + right + 1;
}
else if(cur > 2)
{
res += (left + 1) * pow(10, index);
}
right += cur * pow(10, index);
index++;
n /= 10;
}
return res;
}
};