题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/
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]
)
解题思路:n=0, 返回1
n=1,返回1+9
n=2,返回1+9+9*9
n=3,返回1+9+9*9*8
....
n=k,返回1+9+9*9*8+...+9*9*8*...*(11-k)
n>10,不再变化
class Solution{
public:
int countNumbersWithUniqueDigits(int n)
{
if(n==0) return 1;
if(n==1) return 10;
int count=10,faciend=9;
for(int i=2;i<=n&&i<=10;i++)
{
faciend*=(11-i);
count+=faciend;
}
return count;
}
};