Count Numbers with Unique Digits

Desicription

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:

1
2
3
4
Input: 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
excluding 11,22,33,44,55,66,77,88,99

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n == 0) {
return 1;
}

int res = 10;
int count = 9;
for(int i = 2; i <= n; i++) {
count *= (11 - i);
res += count;
}

return res;
}
};