-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (30 loc) · 853 Bytes
/
Copy pathsolution.java
File metadata and controls
34 lines (30 loc) · 853 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
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/
// Medium | Java | Accepted 2025-12-04
// Runtime 2 ms | Memory 45.7 MB
class Solution {
List<List<Integer>> ans = new ArrayList<>();
int[] cand;
int t;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
cand = candidates;
t = target;
recurse(0, 0, new ArrayList<>());
return ans;
}
public void recurse(int index, int sum, ArrayList<Integer> curr)
{
if(index >= cand.length || sum >= t)
{
if(sum==t)
{
ans.add(new ArrayList<>(curr));
}
return;
}
curr.add(cand[index]);
recurse(index, sum+cand[index], curr);
curr.remove(curr.size()-1);
recurse(index+1, sum, curr);
}
}