357. Count Numbers with Unique Digits

class Solution:
    def countNumbersWithUniqueDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        # 解法来源https://leetcode.com/problems/count-numbers-with-unique-digits/discuss/83040/Simple-Python-solution-90
        # choices是有几位的可能性,本身ans = 1代表0这个数,
        # 一位1~9,两位第二位不能和第一位一样,9种可能
        # 三位第三位不能和前两位一样,8种可能……
        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
    

 

你可能感兴趣的:(python)