-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinations.java
More file actions
29 lines (28 loc) · 853 Bytes
/
Combinations.java
File metadata and controls
29 lines (28 loc) · 853 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.ArrayList;
import java.util.List;
public class Combinations {
public static void main(String[] args) {
int n=4,k=2;
System.out.println(combine(n, k));
}
public static List<List<Integer>> combine(int n, int k) {
List<Integer> ls=new ArrayList<>();
List<List<Integer>> res=new ArrayList<>();
for(int i=1;i<=n;i++){
ls.add(i);
}
solve(k,ls,0,new ArrayList<>(),res);
return res;
}
public static void solve(int k,List<Integer> ls,int i,List<Integer> ds,List<List<Integer>> res){
if(ds.size()==k){
res.add(new ArrayList<>(ds));
return;
}
if(i==ls.size()) return;
ds.add(ls.get(i));
solve(k,ls,i+1,ds,res);
ds.remove(ds.size()-1);
solve(k,ls,i+1,ds,res);
}
}