Problem :
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])
**
Algorithm:
**
可以把n看成位数,设F(k)表示k位数中有多少个数是符合条件的,那么我们要求得结果就变成了F(k)+F(k-1)+F(k-2)+……+F(1),而F(k)=9*9*8*……(9-k+2)
**
Code:
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n==0)
return 1;
if(n==1)
return 10;
int a=10;
for(int j=2;j<=n;j++)
{
int sum=9;
for(int i=2;i<=j;i++)
{
sum*=11-i;
}
a+=sum;
}
return a;
}
};