Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.31 KB

357-count-numbers-with-unique-digits.md

File metadata and controls

47 lines (33 loc) · 1.31 KB

357. Count Numbers with Unique Digits - 计算各个位数不同的数字个数

给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10

示例:

输入: 2
输出: 91 
解释: 答案应为除去 11,22,33,44,55,66,77,88,99 外,在 [0,100) 区间内的所有数字。

题目标签:Math / Dynamic Programming / Backtracking

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
python3 32 ms N/A
class Solution:
    def countNumbersWithUniqueDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 0:
            return 1
        else:
            n = min(n, 10)
            arr = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1]
            a = self.countNumbersWithUniqueDigits(n-1)
            b = 1
            for i in range(n):
                b *= arr[i]
            return a + b