Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical (dictionary) order. You must write an algorithm that runs in O(n) time and uses O(1) extra space.
Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical (dictionary) order. You must write an algorithm that runs in O(n) time and uses O(1) extra space.
Example 1:
Input: {"n":13}
Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
Constraints:
1≤n≤5×104
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"n":13}
List 1..13 in dictionary order, as if they were strings. Picture a trie: 1 to 9 at the top, and under every number x its children x0 to x9. Dictionary order is a preorder walk of that trie.