Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
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
参考资料:here。
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n == 0) return 1;
vector<int> dp(10);
dp[0] = 9;
for (int i = 1; i < 10; i++) dp[i] = dp[i - 1] * (10 - i);
int ans = 0;
for (int i = 0; i < n; i++) ans += dp[i];
return ans+1;
}
};