-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC386.java
More file actions
54 lines (44 loc) · 1.41 KB
/
LC386.java
File metadata and controls
54 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* LC386
*/
import java.util.*;
// import java.util.stream.Collectors;
public class LC386 {
public static List<Integer> lexicalOrder(int n) {
// ? This type-1
// List<String> nums = new ArrayList<>();
// for (int i = 1; i <= n; i++) {
// nums.add(String.valueOf(i));
// }
// Collections.sort(nums);
// return nums.stream().map(Integer::parseInt).collect(Collectors.toList());
// ? This type-2
int numberToPrint = 1;
List<Integer> nums = new ArrayList<>();
for (int i = 0; i < n; i++) {
nums.add(numberToPrint);
if (numberToPrint * 10 <= n) {
numberToPrint *= 10;
} else {
if (numberToPrint + 1 <= n && numberToPrint % 10 != 9) {
numberToPrint++;
} else {
while (((numberToPrint / 10) % 10) == 9) {
numberToPrint = numberToPrint / 10;
}
numberToPrint = numberToPrint / 10 + 1;
}
}
}
return nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number here : ");
int n = sc.nextInt();
System.out.println();
List<Integer> ans = lexicalOrder(n);
System.out.println(ans);
sc.close();
}
}