Given an integer n, return 1 - n in lexicographical order.
For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].
Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.
my solution
1
return sorted([i for i in range(1,n+1)],key=str)
a clear solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14def order(n):
out = []
cur = 1
for i in range(1,n+1):
out.append(cur)
if cur*10 <= n:
cur *= 10
elif cur%10 != 9 and cur + 1 <= n:
cur += 1
else:
while (cur//10)%10 == 9:
cur = cur//10
cur = cur//10 + 1
return out