Leetcode 386 Solution
This article provides solution to leetcode question 386 (lexicographical-numbers)
Access this page by simply typing in "lcs 386" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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