0%

Leetcode386-Lexicographical Numbers

Description

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.

Solution

  • Time Complexity:
  • Space Complexity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> res = new ArrayList<>();
for (int i = 1; i <= 9; i++){
helper(i, n, res);
}
return res;
}

private void helper(int cur, int n, List<Integer> res){
if (cur > n){
return;
}

res.add(cur);
for (int i = 0; i <= 9; i++){
int newInt = cur * 10 + i;
if (newInt > n) return;
helper(newInt, n, res);
}
}
}