-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination Sum.java
57 lines (41 loc) · 1.79 KB
/
Combination Sum.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://leetcode.com/problems/combination-sum/
class Solution {
public void makeCombinations(List<List<Integer>> res, LinkedList<Integer> combo, int[] candidates, int target, int beginIT) {
// If target is less than 0, exit function
if (target < 0) {
return;
}
// If target has been reached, add combo to res and exit function
else if (target == 0) {
res.add(new LinkedList<Integer>(combo));
return;
}
// Iterate candidates
for (int i=beginIT; i<candidates.length; i++) {
// Add candidate to combo
combo.add(candidates[i]);
// Make a recursive call
// NOTE: Update target by subtracting ith element from it
makeCombinations(res, combo, candidates, target - candidates[i], i);
// Remove ith candidate from combo
combo.remove(combo.size() - 1);
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
// If candidates only has 1 element
if (candidates.length == 1 && candidates[0] == target) {
// Create a linked list and save target in it
List<Integer> ll = new LinkedList<>();
ll.add(target);
// Save above linked list inside another linked list
List<List<Integer>> ans = new LinkedList<>();
ans.add(ll);
return ans;
}
// Create a list
List<List<Integer>> res = new LinkedList<>();
// Make a list of combos
makeCombinations(res, new LinkedList<Integer>(), candidates, target, 0);
return res;
}
}