-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
38 lines (32 loc) · 1.28 KB
/
Combinations.java
File metadata and controls
38 lines (32 loc) · 1.28 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
package solutions;
import java.util.*;
// [Problem] https://leetcode.com/problems/combinations
class Combinations {
// Backtracking
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> combinations = new ArrayList<>();
backtrack(combinations, new LinkedList<>(), k, 1, n);
return combinations;
}
private void backtrack(List<List<Integer>> combinations, LinkedList<Integer> combination, int len, int next, int max) {
if (combination.size() == len) {
combinations.add(new LinkedList<>(combination));
return;
}
for (int num = next; num <= max; num++) {
combination.add(num);
backtrack(combinations, combination, len, num + 1, max);
combination.removeLast();
}
}
// Test
public static void main(String[] args) {
Combinations solution = new Combinations();
// order doesn't matter
Set<List<Integer>> expectedOutput = Set.of(
List.of(2, 4), List.of(3, 4), List.of(2, 3),
List.of(1, 2), List.of(1, 3), List.of(1, 4));
Set<List<Integer>> actualOutput = new HashSet<>(solution.combine(4, 2));
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}