class Solution:
def lexicalOrder(self, n: int) -> List[int]:
#1, 10, 11, 12, 13 ...,100,
if n < 1:
return []
ans = [1]
while len(ans) < n: #1
new = ans[-1] * 10 #10
while new > n:
new //= 10 # new is ans[-1]
new += 1 #
while new % 10 == 0: #not inclue 0, so 10, 100, 1000,
new //= 10 # until 2,3,4,5,...
ans.append(new) #10 if n is 11, 2 if n is 9
return ans
Time O(n). Space O(1).