Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
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])
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];
}
};