A/C Python solution, easy to understand, recursive solution


  • 0
    W

    class Solution(object):
    def countNumbersWithUniqueDigits(self, n):
    """
    :type n: int
    :rtype: int
    """

        def helper(n):            
        
            if n == 0:
                return 1
            elif n == 1:
                return 10
    
            res1 = 1
            for i in range(n):
                #get all numbers
                res1 = res1 *(10-i)
    
            #print "res1 = ", res1
    
            res2 = 1
            for i in range(n-1):
                #find the numbers with duplicate digits
                res2 = res2*(9-i)
    
            #print "res2 = ", res2
        
            return res1-res2+helper(n-1)
        
        return helper(n)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.