zoukankan      html  css  js  c++  java
  • 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)






  • 相关阅读:
    SSH出现ls command not found
    SVN打包备份
    【转】Linux安装JDK1.7 prm
    任务
    java多线程
    JAVA开发中151个建议
    Linux Too Many OpenFiles
    【收藏】Linux tail命令
    Linux读取属性配置文件注意事项
    [转]Linux端口查看命令
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8445742.html
Copyright © 2011-2022 走看看