39. Combination Sum
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.
- 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] ]
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { Arrays.sort(candidates); ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>(); combinationSum(candidates, 0, target, new LinkedList<Integer>(), ret); return ret; } private void combinationSum(int[] candidates, int from, int target, LinkedList<Integer> current, ArrayList<List<Integer>> ret) { for(int i = from; i<candidates.length; ++i) { int c = candidates[i]; if(c > target) return; if(c == target) { LinkedList<Integer> sol = new LinkedList<Integer>(current); sol.add(c); ret.add(sol); return; } current.addLast(c); combinationSum(candidates, i, target -c, current, ret); current.removeLast(); } } }
40. 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.
- 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] ]
public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>(); combinationSum(candidates, 0, target, new LinkedList<Integer>(), ret); return ret; } private void combinationSum(int[] candidates, int from, int target, LinkedList<Integer> current, ArrayList<List<Integer>> ret) { Integer lastRemoved = null; for(int i = from; i<candidates.length; ++i) { int c = candidates[i]; if(new Integer(c).equals(lastRemoved)) //if(new Integer(c) == lastRemoved) //This is doing a reference equality check. //if(lastRemoved == c) //This give a java.lang.NullPointerException continue; if(c > target) return; if(c == target) { LinkedList<Integer> sol = new LinkedList<Integer>(current); sol.add(c); ret.add(sol); return; } current.addLast(c); combinationSum(candidates, i+1, target -c, current, ret); lastRemoved = current.removeLast(); } } }
216. Combination Sum III
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.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]]
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { int[] candidates = new int[]{1,2,3,4,5,6,7,8,9}; int target = n; int count = k; ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>(); combinationSum(count, candidates, 0, target, new LinkedList<Integer>(), ret); return ret; } private void combinationSum(int count, int[] candidates, int from, int target, LinkedList<Integer> current, ArrayList<List<Integer>> ret) { Integer lastRemoved = null; for(int i = from; i<candidates.length; ++i) { Integer c = candidates[i]; if(c.equals(lastRemoved)) continue; if(c > target) return; if(c.equals(target)) { if(count>1) return; LinkedList<Integer> sol = new LinkedList<Integer>(current); sol.add(c); ret.add(sol); return; } if(count<=1) continue; current.addLast(c); combinationSum(--count, candidates, i+1, target -c, current, ret); ++count; lastRemoved = current.removeLast(); } } }
Solution2:
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> ans = new ArrayList<>(); combination(ans, new ArrayList<Integer>(), k, 1, n); return ans; } private void combination(List<List<Integer>> ans, List<Integer> comb, int k, int start, int n) { if (comb.size() == k) { if(n == 0) { List<Integer> li = new ArrayList<Integer>(comb); ans.add(li); } return; } for (int i = start; i <= 9; i++) { if(i>n) return; comb.add(i); combination(ans, comb, k, i+1, n-i); comb.remove(comb.size() - 1); } } }
377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
public class Solution { public int combinationSum4(int[] nums, int target) { List<Integer> newNums = new ArrayList<>(); for (Integer n : nums) if (n <= target) newNums.add(n); Collections.sort(newNums); int[] results = new int[target + 1]; for (int sum = 1; sum < results.length; ++sum) { for (int n : newNums) { if (n > sum) break; else if (n == sum) results[sum] += 1; else results[sum] += results[sum - n]; } } return results[target]; } }