美文网首页
357. Count Numbers with Unique D

357. Count Numbers with Unique D

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-07-25 15:50 被阅读0次

    357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits
    class Solution(object):
        def countNumbersWithUniqueDigits(self, n):
            """
            :type n: int
            :rtype: int
            """
            res = 0
            if n == 0:
                return 1
            while n > 0:
                res += self.helper(n)
                n -= 1
            return res
        
        def helper(self, n):
            if n == 1:
                return 10
            res = 9*9
            n -= 2
            temp = 8
            while n > 0:
                n -= 1
                res *= temp
                temp -= 1
            return res
    

    我写的代码真是没啥美感,来份大佬的看下吧。

    class Solution(object):
        def countNumbersWithUniqueDigits(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n == 0:
                return 1
            g, h = 10, 9
            for i in range(n-1):
                g += 9*h
                h *= (8-i)
            return(g)
    

    相关文章

      网友评论

          本文标题:357. Count Numbers with Unique D

          本文链接:https://www.haomeiwen.com/subject/jdyhrctx.html