题目 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位数的unique digits数量为f(n) f(0)=1; f(1)=9; f(2)=9*9; f(3)=9*9*8; … f(10)=9*9*8*…*1; n>10时f(n) = f(10); 故 0 ≤ x < 10n为所表示小于n位数字的f的和代码 int countNumbersWithUniqueDigits(int n) { if(n==0) return 1; if(n==1) return 10; int nUniq = 9; int nLeft = 9; int nCount = 10; while((n--)>1 && nLeft>1) { nUniq*=nLeft; nCount+=nUniq; --nLeft; } return nCount; }