-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
32 lines (26 loc) · 839 Bytes
/
CombinationSum.java
File metadata and controls
32 lines (26 loc) · 839 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
package com.mirraico.leetcode;
import java.util.*;
public class CombinationSum {
public void combine(int[] candidates, int pos, int target, int count,
List<Integer> tmpAns, List<List<Integer>> ans) {
if(count > target) return;
if(count == target) {
List<Integer> cloneAns = new ArrayList<>(tmpAns);
ans.add(cloneAns);
return;
}
for(int i = pos; i < candidates.length; i++) {
tmpAns.add(candidates[i]);
this.combine(candidates, i, target, count + candidates[i], tmpAns, ans);
tmpAns.remove(tmpAns.size() - 1);
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> tmpAns = new ArrayList<>();
this.combine(candidates, 0, target, 0, tmpAns, ans);
return ans;
}
public static void main(String[] args) {
}
}