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.
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]
class Solution {
public:
void combination(vector<vector<int>>& result,vector<int>& candidates,vector<int> cur,int target,int begin){
if(target == 0){
result.push_back(cur);
return;
}
for(int i = begin;i < candidates.size();i++){
if(candidates[i] <= target){
cur.push_back(candidates[i]);
combination(result,candidates,cur,target-candidates[i],i); //最后一个参数是i,因为允许重复
cur.pop_back();
}
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
if(candidates.empty() || target <= 0) return result;
sort(candidates.begin(), candidates.end());
vector<int> cur;
combination(result,candidates,cur,target,0);
return result;
}
};
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.
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]
class Solution {
public:
void combinations(vector<vector<int>>& result, vector<int>& candidates, vector<int> cur, int target, int begin) {
if (target == 0) {
result.push_back(cur);
return;
}
for (int i = begin; i < candidates.size(); i++) {
if (candidates[i] <= target) {
if (i == begin || candidates[i] != candidates[i - 1]) {//去掉重复值
cur.push_back(candidates[i]);
combinations(result, candidates, cur, target - candidates[i], i + 1); //不允许重复
cur.pop_back();
}
}
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> result;
if (candidates.empty() || target <= 0) return result;
vector<int> cur;
sort(candidates.begin(), candidates.end());
combinations(result, candidates, cur, target, 0);
return result;
}
};
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.
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]]
这是一道回溯题,其实还比较简单。
解法如下:
class Solution {
public:
void helper(int k, int n, vector<vector<int>>& result, vector<int>& cur){
if(cur.size() == k && n ==0){
result.push_back(cur);
return ;
}
int i;
if(cur.size() <=0) i =1;
else i = cur.back()+1; //+1
for(; i <=9;i++){
if(n -i < 0) break;
cur.push_back(i);
helper(k,n-i,result,cur);
cur.pop_back();
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> result;
if(k<=0 || n <=0 || k > n) return result;
vector<int> cur;
helper(k,n,result,cur);
return result;
}
};
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?
DP算法
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> result(target+1,0);
result[0] = 1; // specially result[0] = 1, the empty combination has sum 0
for(int i =1 ; i <= target;i++){
for(auto num : nums){
if(i >= num){
result[i] += result[i-num];
}
}
}
return result[target];
}
};