class Count2 {
public:
int countNumberOf2s(int n) {
// write code here
int low = 0;
int high = n / 10;
int cur = n % 10;
int digit = 1;
int count = 0;
while (high || cur) {
if (cur < 2) {
count += high * digit;
} else if (cur == 2) {
count += high * digit + 1 + low;
} else {
count += (high + 1) * digit;
}
low +=cur*digit;
cur = high%10;
high /=10;
digit *=10;
}
return count;
}
};

京公网安备 11010502036488号