Python solution with detailed explanation


  • 0
    G

    Solution

    Lexicographical Numbers https://leetcode.com/problems/lexicographical-numbers/

    class Solution(object):
        def helper(self, curr, result, n):
            result.append(curr)
            start = 1 if curr == 0 else 0
            for i in range(start, 10):
                if curr * 10 + i <= n:
                    self.helper(curr*10 + i, result, n)
            return
        
        def lexicalOrder(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
            result = []
            self.helper(0, result, n)
            return result[1:]
    

Log in to reply
 

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