-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine77.java
More file actions
29 lines (27 loc) · 822 Bytes
/
combine77.java
File metadata and controls
29 lines (27 loc) · 822 Bytes
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
import java.util.*;
public class combine77 {
static List<List<Integer>> result = new ArrayList<>();
public static void main(String[] args) {
System.out.println(combine(4,2));
}
public static List<List<Integer>> combine(int n, int k) {
int[] number = new int[n];
for(int i = 1; i <= n; i++) {
number[i - 1] = i;
}
helper(1, n, k, new ArrayList<Integer>());
return result;
}
public static void helper(int i, int n, int k, List<Integer> list) {
if(list.size() == k) {
result.add(new ArrayList<>(list));
}
else {
for(int j = i; j <= n; j++) {
list.add(j);
helper(j +1, n, k, list);
list.remove(list.size() - 1);
}
}
}
}