LeetCode 357. Count Numbers with Unique Digits

考点难度
DPMedium
题目

Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10^n.

思路

第一个数字有9个选择,第二个有9个,第三个有8个,etc

答案
class Solution(object):
    def countNumbersWithUniqueDigits(self, n):
        choices = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1]
        ans, product = 1, 1
        
        for i in range(n if n <= 10 else 10):
            product *= choices[i]
            ans += product
            
        return ans
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐