357. Count Numbers with Unique Digits [Medium] 数学

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)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容