Repository navigation
Expand file tree
/
Copy pathAllCombinations.java
More file actions
63 lines (56 loc) · 2.13 KB
/
Copy pathAllCombinations.java
File metadata and controls
63 lines (56 loc) · 2.13 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
55
56
57
58
59
60
61
62
63
package Leetcode;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*
This class generates all possible combinations of length k from a set of integers 1 to n.
*/
public class AllCombinations {
/**
Generates all possible combinations of k numbers out of 1...n.
@param n The maximum number to consider for the combinations.
@param k The size of each combination.
@return A list of all possible combinations of k numbers out of 1....n.
*/
public static List<List<Integer>> generateCombinations(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), 1, n, k);
return result;
}
/**
Helper method to generate all possible combinations of k numbers out of 1....n.
@param result A list to store all the generated combinations.
@param temp A temporary list to store each combination during the recursive process.
@param start The starting number to consider for the current combination.
@param n The maximum number to consider for the combinations.
@param k The size of each combination.
*/
private static void backtrack(List<List<Integer>> result, List<Integer> temp, int start, int n, int k) {
if (temp.size() == k) {
result.add(new ArrayList<>(temp));
return;
}
for (int i = start; i <= n; i++) {
temp.add(i);
backtrack(result, temp, i + 1, n, k);
temp.remove(temp.size() - 1);
}
}
@Test
public void testGenerateCombinations() {
List<List<Integer>> expected = List.of(
List.of(1, 2, 3),
List.of(1, 2, 4),
List.of(1, 2, 5),
List.of(1, 3, 4),
List.of(1, 3, 5),
List.of(1, 4, 5),
List.of(2, 3, 4),
List.of(2, 3, 5),
List.of(2, 4, 5),
List.of(3, 4, 5)
);
List<List<Integer>> actual = AllCombinations.generateCombinations(5, 3);
assertEquals(expected, actual);
}
}