-
Notifications
You must be signed in to change notification settings - Fork 0
/
39.java
29 lines (28 loc) · 796 Bytes
/
39.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
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private List<Integer> path = new ArrayList<>();
private int[] nums;
private Map<Integer,Integer> map = new HashMap<>();
private int target;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
nums = candidates;
this.target = target;
dfs(0,0);
return ans;
}
private void dfs(int sum,int length){
if(sum>target){
return;
}
if(sum==target){
ans.add(new ArrayList<>(path));
}
for(int i = length;i<nums.length;i++){
path.add(nums[i]);
sum+=nums[i];
dfs(sum,i);
sum-=nums[i];
path.remove(path.size()-1);
}
}
}