357. Count Numbers with Unique Digits 用唯一的数字计算数字

Posted Long Long Journey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

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)




以上是关于357. Count Numbers with Unique Digits 用唯一的数字计算数字的主要内容,如果未能解决你的问题,请参考以下文章

357. Count Numbers with Unique Digits

leetcode [357]Count Numbers with Unique Digits

[leetcode-357-Count Numbers with Unique Digits]

[LeetCode] 357. Count Numbers with Unique Digits

leetCode 357. Count Numbers with Unique Digits | Dynamic Programming | Medium

357. Count Numbers with Unique Digits 用唯一的数字计算数字