-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
40 lines (33 loc) · 1.33 KB
/
CombinationSum.java
File metadata and controls
40 lines (33 loc) · 1.33 KB
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
37
38
39
40
package BackTracking;
import java.util.*;
public class CombinationSum {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
backtrack(candidates, target, 0, new ArrayList<>(), list);
return list;
}
private void backtrack(int[] candidates, int target, int start, List<Integer> subList, List<List<Integer>> list) {
if (target == 0) {
list.add(new ArrayList<>(subList)); // add a copy of current combination
return;
}
for (int i = start; i < candidates.length; i++) {
if (candidates[i] <= target) {
subList.add(candidates[i]);
backtrack(candidates, target - candidates[i], i, subList, list); // allow reuse of same element
subList.remove(subList.size() - 1); // backtrack
}
}
}
// Main method to test
public static void main(String[] args) {
CombinationSum sol = new CombinationSum();
int[] candidates = {2, 3, 6, 7};
int target = 7;
List<List<Integer>> result = sol.combinationSum(candidates, target);
System.out.println("Combinations that sum to " + target + ":");
for (List<Integer> combo : result) {
System.out.println(combo);
}
}
}