Code Time Complexity: O(n), Space Complexity: O(1) 排列組合問題,不重複的排法是階層。 class Solution { public: int countNumbersWithUniqueDigits(int n) { if(n == 0) return 1; if(n == 1) return 10; int res = 10; int choice = 9; int accumulate = 9; for(int i = 2; i <= n; i++) { accumulate *= choice; res += accumulate; choice--; } return res; } }; Link Count Numbers with Unique Digits