Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
mengli committed Jan 14, 2014
1 parent 52b74e9 commit 7af17b5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
30 changes: 10 additions & 20 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


import java.util.ArrayList;
import java.util.Arrays;

Expand All @@ -11,14 +10,13 @@
*
* Note:
*
* All numbers (including target) will be positive integers.
* 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).
* 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]
* 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]
*/

public class CombinationSum {
Expand All @@ -38,20 +36,12 @@ private void combinationSum(int[] candidates, int start, int sum,
ret.add(new ArrayList<Integer>(solution));
return;
}
if (start > candidates.length - 1)
if (sum > target)
return;
int times = 0;
while (true) {
if (sum > target) {
for (int h = 0; h < times; h++) {
solution.remove(solution.size() - 1);
}
break;
}
combinationSum(candidates, start + 1, sum, target, ret, solution);
sum += candidates[start];
solution.add(candidates[start]);
times++;
for (int i = start; i < candidates.length; i++) {
solution.add(candidates[i]);
combinationSum(candidates, start, sum + candidates[i], target, ret, solution);
solution.remove(solution.size() - 1);
}
}
}
50 changes: 50 additions & 0 deletions CombinationSumII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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]
*/

import java.util.ArrayList;
import java.util.Arrays;

public class CombinationSumII {
public ArrayList<ArrayList<Integer>> combinationSum2(int[] candidates,
int target) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list = new ArrayList<Integer>();
Arrays.sort(candidates);
dfs(res, 0, candidates, list, target);
return res;
}

private void dfs(ArrayList<ArrayList<Integer>> res, int start,
int[] candidates, ArrayList<Integer> list, int target) {
if (target == 0) {
res.add(new ArrayList<Integer>(list));
return;
}
int pre = -1;
for (int i = start; i < candidates.length; i++) {
if (pre == candidates[i])
continue;
if (candidates[i] > target)
return;
pre = candidates[i];
list.add(candidates[i]);
dfs(res, i + 1, candidates, list, target - candidates[i]);
list.remove(list.size() - 1);
}
}
}

0 comments on commit 7af17b5

Please sign in to comment.