Leetcode 386 Solution

This article provides solution to leetcode question 386 (lexicographical-numbers)

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

Solution

class Solution:
    def lexicalOrder(self, n: int) -> List[int]:
        ans = []

        def dfs(v, n, allow_zero=False):
            if v > n:
                return

            if allow_zero:
                ans.append(v)

            if allow_zero:
                dfs(v * 10, n, True)

            for i in range(1, 10):
                dfs(v * 10 + i, n, True)

        dfs(0, n)

        return ans