Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak). The solution set must not contain duplicate combinations. For example, given candidate set 2,3,6,7 and target 7, A solution set is: [7] [2, 2, 3]
Solution
Simple DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
publicstatic List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> tmp = new ArrayList<Integer>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
dfs(res, tmp, candidates, target, 0);
return res;
}
privatestaticvoiddfs(List<List<Integer>> res, List<Integer> tmp, int[] candidates, int target, int start){
if(target==0) {
res.add(new ArrayList<Integer>(tmp));
return;
}
if(target<0) return;
for(int i=start;i<candidates.length;i++){
tmp.add(candidates[i]);
dfs(res,tmp,candidates,target-candidates[i],i);
tmp.remove(tmp.size()-1);
}
}
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak). The solution set must not contain duplicate combinations. For example, given candidate set 10,1,2,7,6,1,5 and target 8, A solution set is: [1, 7] [1, 2, 5] [2, 6] [1, 1, 6]
Solution
Two small changes:
pass in i+1 as new start in each recursive call
keep track of a prev value of the last value in the last tuple, if the next one equals to prev then it is duplicate
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
publicstatic List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<Integer> tmp = new ArrayList<Integer>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
dfs2(res, tmp, candidates, target, 0);
return res;
}
privatestaticvoiddfs2(List<List<Integer>> res, List<Integer> tmp, int[] candidates, int target, int start){
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
publicstatic List<List<Integer>> combinationSum3(int k, int n) {
List<Integer> tmp = new ArrayList<Integer>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
dfs(res,tmp,k,n,1);
return res;
}
privatestaticvoiddfs(List<List<Integer>> res, List<Integer>tmp, int k, int n, int start){