Python DFS solution


  • 0
    L
    class Solution(object):
        def lexicalOrder(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
            self.result = []
            self.helper(0, 1, n)
            return self.result
        
        def helper(self, base, level, n):
            if base*10 > n:
                return
            for i in xrange(0, 10):
                if level == 1 and i == 0: # should not do 0, 00, 000
                    continue
                thisNum = base * 10 + i
                if thisNum > n:
                    return
                self.result.append(thisNum)
                self.helper(base * 10 + i, level + 1, n)

Log in to reply
 

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