给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n。
示例:
给定 n = 2,返回 91。(答案应该是除[11,22,33,44,55,66,77,88,99]外,0 ≤ x < 100 间的所有数字)
详见:https://leetcode.com/problems/count-numbers-with-unique-digits/description/
C++:
class Solution { public: int countNumbersWithUniqueDigits(int n) { if (n == 0) { return 1; } int res = 0; for (int i = 1; i <= n; ++i) { res += count(i); } return res; } int count(int k) { if (k < 1) { return 0; } if (k == 1) { return 10; } int res = 1; for (int i = 9; i >= (11 - k); --i) { res *= i; } return res * 9; } };
参考:https://www.cnblogs.com/grandyang/p/5582633.html