Given an integer n
, return all the numbers in the range [1, n]
sorted in lexicographical order.
You must write an algorithm that runs in O(n)
time and uses O(1)
extra space.
For example:
n = 13
, the output should be [1,10,11,12,13,2,3,4,5,6,7,8,9]
n = 2
, the output should be [1,2]
Can you provide an efficient algorithm to solve this problem?
A straightforward approach is to generate all numbers from 1 to n
, convert them to strings, and then sort the strings lexicographically. Finally, convert the sorted strings back to integers.
n
.def lexicalOrder_naive(n):
nums = list(range(1, n + 1))
nums_str = [str(num) for num in nums]
nums_str.sort()
result = [int(num_str) for num_str in nums_str]
return result
A more efficient approach is to use Depth-First Search (DFS) to generate the numbers in lexicographical order directly. We can think of this as traversing a n-ary tree where each node's children are its multiples of 10.
n
).n
, backtrack and try incrementing the current number until it is less than 10.[1, n]
have been added.def lexicalOrder(n):
result = []
def dfs(num):
if num > n:
return
result.append(num)
for i in range(10):
next_num = num * 10 + i
if next_num > n:
break
dfs(next_num)
for i in range(1, 10):
if i <= n:
dfs(i)
return result
# An iterative solution:
def lexicalOrder_iterative(n):
result = []
curr = 1
for _ in range(n):
result.append(curr)
if curr * 10 <= n:
curr *= 10
else:
if curr >= n:
curr //= 10
curr += 1
while curr % 10 == 0:
curr //= 10
return result
[1]
[1,2,3,4,5,6,7,8,9]
[1,10,2,3,4,5,6,7,8,9]
n
efficiently without exceeding memory limits or execution time limits.