-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.java
More file actions
23 lines (21 loc) · 780 Bytes
/
39.java
File metadata and controls
23 lines (21 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
Map<Integer, List<Integer>> map = new HashMap<>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public void find(int[] nums, int target, int index, ArrayList<Integer> condition) {
if (target == 0) {
ans.add(new ArrayList<Integer>(condition));
return;
}
for (int i = index; i < nums.length; i++) {
if (target >= nums[i]) {
condition.add(nums[i]);
find(nums, target - nums[i], i, condition);
condition.remove(condition.size() - 1);
}
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
find(candidates, target, 0, new ArrayList<Integer>());
return ans;
}
}