Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
題目的意思就是每一位上的數字都不能重複!!!動態規劃思想:
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n == 0)return 1;
vector<int>re(n + 1, 0);
re[1] = 10;
for (int i = 2; i <= n; i++){
int start = 9;
int bit = i - 1;
int sum = 0;
int pro = 9;
while (bit){
pro = pro*start;
start--;
if(n>1)sum = sum + re[bit]-re[bit-1];
else sum = sum + re[1];
bit--;
}
re[i] = pro + sum;
}
return re[n];
}
};