O(1) Python solution with cheat sheet :P


  • 3
    cheat_sheet = [1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771]
    class Solution(object):
        def countNumbersWithUniqueDigits(self, n):
            return cheat_sheet[n] if n<11 else cheat_sheet[10]

  • 0
    G

    @MingHan1990 I guess none of the test cases check for values of n greater than or equal to 11. Otherwise, you'd have an IndexError. There are only 10 elements in the array and the last element is at index 9. Either delete the else entirely or change it to cheat_sheet[9]


  • 0

    Thx for correcting my error~


  • 0

    @grodrigues3 Thx for correcting my error~


  • 0
    V

    I feel this will look nicer:

    def countNumbersWithUniqueDigits(self, n):
        return cheat_sheet[min(n, 10)]
    

    Edit:
    Why the down vote?


Log in to reply
 

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