Lexicographical Numbers
Given an integer n, return all numbers from 1 to n in lexicographical order.
Lexicographical (dictionary) order compares numbers as strings: 1, 10, 11, 12, 2, ... because the character '1' is compared before '2'.
Examples
Input: []
Output: 0
Input: ["1"]
Output: 1
Hints
Lexicographical order follows the same structure as a trie of decimal digits.
You can generate numbers in order by trying to append digits `0-9` (depth-first) and backtracking when values exceed `n`.
An iterative solution can walk the implicit trie by multiplying by 10 when possible, otherwise incrementing and removing trailing 9s.
Related Problems
Lexicographical Numbers
Given an integer `n`, return all numbers from `1` to `n` in lexicographical order.