Leetcode[39, 40, 216]Combination Sum
19 Nov 2015###Task1 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]
###Python
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
ret = []
temp = []
self.helper(ret, temp, sorted(candidates), target)
return ret
def helper(self, ret, temp, candidates, target):
if target == 0:
ret.append(temp[:])
return
for i, num in enumerate(candidates):
if num > target:
return
temp.append(num)
self.helper(ret, temp, candidates[i:], target - num)
temp.pop()
###Java
public class Solution {
public void combinator(int[] candidates, int target, ArrayList<List<Integer>> res, int pos, ArrayList<Integer> temp) {
if (target == 0) {
//cannot add temp directly !! its a object reference !!
ArrayList<Integer> current = new ArrayList<Integer>(temp);
res.add(current);
return;
}
for (int i = pos; i < candidates.length; i++) {
if (candidates[i] > target) {
return;
}
temp.add(candidates[i]);
combinator(candidates, target - candidates[i], res, i, temp);
temp.remove(temp.size() - 1);
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
//edge cases
if (candidates == null || candidates.length == 0) {
return null;
}
Arrays.sort(candidates);
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
combinator(candidates, target, res, 0, temp);
return res;
}
}
###Task2 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]
###Python ```pythonclass Solution(object): def combinationSum2(self, candidates, target): “”” :type candidates: List[int] :type target: int :rtype: List[List[int]] “”” ret = [] temp = [] self.helper(ret, temp, sorted(candidates), target) return ret def helper(self, ret, temp, candidates, target): if target == 0: ret.append(temp[:]) return for i, num in enumerate(candidates): if num > target: return if i != 0 and candidates[i] == candidates[i - 1]: continue temp.append(num) self.helper(ret, temp, candidates[i + 1:], target - num) temp.pop()
###Java
```java
public class Solution {
public void combinationHelper(int[] candidates, List<List<Integer>> res, List<Integer> temp, int target, int pos) {
if (target == 0) {
res.add(new ArrayList<Integer>(temp));
return;
}
for (int i = pos; i < candidates.length; i++) {
if (candidates[i] > target) {
break;
}
if (i != pos && candidates[i] == candidates[i - 1]) {
continue;
}
temp.add(candidates[i]);
combinationHelper(candidates, res, temp, target - candidates[i], i + 1);
temp.remove(temp.size() - 1);
}
return;
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if (candidates == null || candidates.length == 0) {
return null;
}
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
combinationHelper(candidates, res, temp, target, 0);
return res;
}
}
###Task3 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]]
###Python
class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
ret = []
self.dfs(ret, k, n, [], 0)
return ret
def dfs(self, ret, k, target, temp, cur):
if len(temp) == k and target == 0:
ret.append(temp[:])
return
for i in range(cur + 1, 10):
temp.append(i)
self.dfs(ret, k, target - i, temp, i)
temp.pop()
###Java
public class Solution {
public void combinationHelper(List<List<Integer>> res, List<Integer> temp, int k, int n, int pos) {
if (temp.size() == k && n == 0) {
res.add(new ArrayList<Integer>(temp));
return;
}
for (int i = pos; i <= 9; i++) {
if (i > n) {
break;
}
temp.add(i);
combinationHelper(res, temp, k, n - i, i + 1);
temp.remove(temp.size() - 1);
}
return;
}
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
combinationHelper(res, temp, k, n, 1);
return res;
}
}