357. 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]
)
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Solution: def countNumbersWithUniqueDigits( self , n): """ :type n: int :rtype: int """ used = [ False for x in range ( 10 )] def gen(used, n, d): if n = = d: return 1 total = 1 startIndex = 1 if d = = 0 else 0 for i in range (startIndex, 10 ): if not used[i]: used[i] = True total + = gen(used, n, d + 1 ) used[i] = False return total return gen(used, n, 0 ) s = Solution() a = s.countNumbersWithUniqueDigits( 7 ) print (a) |