39 . Combination Sum
Medium
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]
]
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
if(candidates==null||candidates.length==0) return res;
combin(res,cur,candidates,0,target);
return res;
}
private void combin(List<List<Integer>> res,List<Integer> cur,int[] can,int start,int target){
if(target==0) res.add(new ArrayList(cur));
for(int i=start;i<can.length;i++){
if(can[i]>target) continue;
cur.add(can[i]);
combin(res,cur,can,i,target-can[i]);
cur.remove(cur.size()-1);
}
}
40 . Combination Sum II
Medium
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]
]
21ms:
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
if(candidates==null||candidates.length==0) return result;
Arrays.sort(candidates);
combin2(result,cur,candidates,0,target);
return result;
}
private void combin2(List<List<Integer>> res,List<Integer> cur,int[] can,int start,int target){
if(target==0) res.add(new ArrayList<Integer>(cur));
int pre = -1;
for(int i=start;i<can.length;i++){
if(can[i]>target) continue;
if(pre==can[i]) continue;
pre = can[i];
cur.add(can[i]);
combin2(res,cur,can,i+1,target-can[i]);
cur.remove(cur.size()-1);
}
}
216 . Combination Sum III
Medium
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]]
1ms:
public List<List<Integer>> combinationSum3(int k, int n) {
int[] book = {1,2,3,4,5,6,7,8,9};
List<List<Integer>> res = new ArrayList<>();
if(9*k<n) return res;
List<Integer> cur = new ArrayList<>();
combin3(k,res,cur,book,n,0);
return res;
}
private void combin3(int k,List<List<Integer>> res,List<Integer> cur,int[] book,int target,int start){
if(target==0&&cur.size()==k) res.add(new ArrayList<Integer>(cur));
for(int i=start;i<book.length;i++){
if(book[i]>target) break;
cur.add(book[i]);
combin3(k,res,cur,book,target-book[i],i+1);
cur.remove(cur.size()-1);
}
}
377 . Combination Sum IV
Medium
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 int combinationSum4(int[] nums, int target) {
int total = 0;
List<Integer> cur = new ArrayList<>();
if(nums.length==0) return 0;
return combin4(total,cur,target,nums);
}
private int combin4(int total,List<Integer> cur,int target,int[] nums){
if(target==0) return total+1;
for(int i=0;i<nums.length;i++){
if(nums[i]>target) continue;
cur.add(nums[i]);
total = combin4(total,cur,target-nums[i],nums);
cur.remove(cur.size()-1);
}
return total;
}
DP:
public int combinationSum4(int[] nums, int target) {
int[] comb = new int[target + 1];
comb[0] = 1;
for (int i = 1; i < comb.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i - nums[j] >= 0) {
comb[i] += comb[i - nums[j]];
}
}
}
return comb[target];
}