给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x <10n 。
示例:
输入:2
输出:91
解释: 答案应为除去 11,22,33,44,55,66,77,88,99 外,在 [0,100) 区间内的所有数字。
Program:
This problem can be seen as dp+mathematics, which is the idea of permutation and combination. For n digits, the beginning of 0 is handed over to the next level (n-1 digits), and the remaining direct permutation and combination are enough, and n cannot be greater than 10.
classSolution{
public:intcountNumbersWithUniqueDigits(int n){
if(n ==0)return1;elseif(n ==1)return10;elseif(n ==2)return91;elseif(n >10)returncountNumbersWithUniqueDigits(10);else{
returncountNumbersWithUniqueDigits(n -1)+9*A(9, n -1);}}intA(int m,int n){
//排列组合计算if(n ==1)return m;elsereturn m *A(m -1, n -1);}};