forked from Soumik-7031/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSumJava
More file actions
23 lines (21 loc) · 774 Bytes
/
combinationSumJava
File metadata and controls
23 lines (21 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private void findCombinations(int ind, int[] arr, int target, List<List<Integer>> ans, List<Integer> ds) {
if(ind == arr.length) {
if(target == 0) {
ans.add(new ArrayList<>(ds));
}
return;
}
if(arr[ind] <= target) {
ds.add(arr[ind]);
findCombinations(ind, arr, target - arr[ind], ans, ds);
ds.remove(ds.size() - 1);
}
findCombinations(ind + 1, arr, target, ans, ds);
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findCombinations(0, candidates, target, ans, new ArrayList<>());
return ans;
}
}