forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination Sum.java
24 lines (24 loc) · 903 Bytes
/
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
// Runtime: 5 ms (Top 69.68%) | Memory: 45.9 MB (Top 24.67%)
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> cur = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
dfs(0, candidates, target, 0, cur, result);
return result;
}
public void dfs(int start, int[] candidates, int target, int sum, List<Integer> cur, List<List<Integer>> result){
if(sum == target){
result.add(new ArrayList<>(cur));
return;
}
for(int i = start; i < candidates.length; i++) {
if(sum + candidates[i] <= target) {
cur.add(candidates[i]);
dfs(i, candidates, target, sum + candidates[i], cur, result);
cur.remove((cur.size()- 1));
}
}
return;
}
}