码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-02-12 23:05:39      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:tps   gen   log   readonly   post   etc   ota   lsp   art   

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 用唯一的数字计算数字

标签:tps   gen   log   readonly   post   etc   ota   lsp   art   

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!