forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum
More file actions
24 lines (20 loc) · 746 Bytes
/
Combination Sum
File metadata and controls
24 lines (20 loc) · 746 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
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
subsets(0, candidates, new ArrayList(), res, target);
return res;
}
void subsets(int index, int[] nums, List<Integer> temp, List<List<Integer>> res, int target){
if(target==0){
res.add(new ArrayList(temp));
}
if(target<0){
return;
}
for(int i = index; i<nums.length; i++){
temp.add(nums[i]);
subsets(i, nums, temp, res, target-nums[i]);
temp.remove(temp.size()-1);
}
}
}