Skip to content

Commit c23d712

Browse files
committed
386. Lexicographical Numbers
1 parent 36b6460 commit c23d712

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ The project is divided into two parts: `structure` and `solution`.
162162
| 31 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | Medium | [NextPermutation.java](src/leetcode/solution/array/NextPermutation.java) | Find the first element that is greater than the previous element, then replace it with the greater and smallest element on the right side. |
163163
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | Easy | [SummaryRanges.java](src/leetcode/solution/array/SummaryRanges.java) | Two Pointer. |
164164
| 281 | [Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/) | Medium | [ZigzagIterator.java](src/leetcode/solution/array/ZigzagIterator.java) | Two Pointer<br />Muti Pointer or Queue for the **Follow-Up** Question. |
165+
| 386 | [Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | Medium | [LexicographicalNumbers.java](src/leetcode/solution/array/LexicographicalNumbers.java) | Recursion |
165166
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | Easy | [FizzBuzz.java](src/leetcode/solution/array/FizzBuzz.java) | For loop. Calculate i%5 and i%3 separately. |
166167
| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | Medium | [OneThreeTwoPattern.java](src/leetcode/solution/array/OneThreeTwoPattern.java) | Using Array as a Stack |
167168
| 768 | [Max Chunks To Make Sorted II](https://leetcode.com/problems/max-chunks-to-make-sorted-ii/) | Hard | [MaxChunksToMakeSortedII.java](src/leetcode/solution/array/MaxChunksToMakeSortedII.java) | Iterate through the array, each time all elements to the left are smaller (or equal) to all elements to the right, there is a new chunk. |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode.solution.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 386. Lexicographical Numbers
8+
*/
9+
public class LexicographicalNumbers {
10+
11+
public static void main(String[] args) {
12+
int n = 13;
13+
LexicographicalNumbersSolution solution = new LexicographicalNumbersSolution();
14+
List<Integer> ans = solution.lexicalOrder(n);
15+
System.out.println(ans);
16+
}
17+
18+
}
19+
20+
class LexicographicalNumbersSolution {
21+
22+
List<Integer> ans;
23+
24+
int n;
25+
26+
public List<Integer> lexicalOrder(int n) {
27+
ans = new ArrayList<>();
28+
this.n = n;
29+
create(0);
30+
return ans;
31+
}
32+
33+
private void create(int base) {
34+
for (int i = 0; i < 10; i++) {
35+
int next = base + i;
36+
if (next > n) {
37+
break;
38+
}
39+
40+
if (next == 0) {
41+
continue;
42+
}
43+
44+
ans.add(next);
45+
create(next * 10);
46+
}
47+
48+
}
49+
}

0 commit comments

Comments
 (0)