My "beats 99%" Python solution


  • 0
    X
    def countNumbersWithUniqueDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 0:
            return 1
        elif n > 10:
            return self.countNumbersWithUniqueDigits(10)
            
        res = [1]
        for i in range(1, n + 1):
            count = res[i - 1] + self.permutation(10, i) - self.permutation(9, i - 1)
            res.append(count)
        return res[-1]
            
    def permutation(self, m, n):
        if m < n or m < 0 or n < 0:
            return -1
            
        res = 1
        for i in range(m, m - n, -1):
            res *= i
        return res

Log in to reply
 

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