-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination.java
More file actions
37 lines (31 loc) · 1003 Bytes
/
Combination.java
File metadata and controls
37 lines (31 loc) · 1003 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
30
31
32
33
34
35
36
package Recursion;
import java.util.ArrayList;
import java.util.List;
/*
77. Combinations
*/
public class Combination {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if(n==0) return res;
List<Integer> subset = new ArrayList<>();
combinnationCreator(1,n,k,subset,res);
return res;
}
public void combinnationCreator(int i,int n, int k,List<Integer> curSet, List<List<Integer>> res){
if(curSet.size()==k){
// Create a copy of the current set and add it to the final response
res.add(new ArrayList<>(curSet));
return;
}
if(i>n){
return;
}
// Decision to add num at i to current set
curSet.add(i);
combinnationCreator(i+1,n,k,curSet,res);
curSet.remove(curSet.size()-1);
// Decision to not add num at i to current set
combinnationCreator(i+1,n,k,curSet,res);
}
}