77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
void dfs_combine(int cnt, int pos,int n,vector<int>&path, vector<vector<int>>& res){
if (cnt == 0){
res.push_back(path);
return;
}
if (pos > n)return;
path.push_back(pos);
dfs_combine(cnt - 1, pos + 1, n, path, res);
path.pop_back();
dfs_combine(cnt, pos + 1, n, path, res);
}
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
if (k <= 0 || n <= 0)return res;
vector<int> path;
dfs_combine(k, 1, n, path, res);
return res;
}
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) 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]
]
DFS的一般模型:
void dfs(int pos, vector<int> tmp, int sum) {
if (Boundary) {
//ok, time to judge
return;
}
//not choose this num
dfs(pos + 1, tmp, sum);
//choose this num
tmp.push_back(a[pos]);
dfs(pos, tmp, sum + a[pos]); // it may be choose again, so position is still this position.
tmp.pop_back();
}
标准的回溯:
void dfs_combinationSum(vector<int>& candidates, int pos, int sum, int target, vector<int>&path, vector<vector<int>>& res){
if (sum > target)return;
if (pos == candidates.size() || sum == target){
if (sum == target)res.push_back(path);
return;
}
dfs_combinationSum(candidates, pos + 1, sum, target, path, res);
path.push_back(candidates[pos]);
dfs_combinationSum(candidates, pos, sum + candidates[pos], target, path, res);
path.pop_back();
}
vector<vector<int>> combinationSum1(vector<int>& candidates, int target) {
vector<vector<int>> res;
if (candidates.size() == 0)return res;
sort(candidates.begin(), candidates.end());
vector<int> path;
dfs_combinationSum(candidates, 0, 0, target, path, res);
return res;
}
第二个写法:
void dfs(vector<int>& candidates, int pos, int sum, int target, vector<int>&path, vector<vector<int>>&result){
if (sum > target)
return;
if (sum == target){
result.push_back(path);
return;
}
for (int i = pos; i < candidates.size(); i++){
path.push_back(candidates[i]);
dfs(candidates, i, sum + candidates[i], target, path, result);
path.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>>result;
vector<int>path;
sort(candidates.begin(), candidates.end());
dfs(candidates, 0, 0, target, path, result);
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.
- 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]
]
要求不能有重复的path,则需要判断当前求出的path是否在原来的vector中包含,即判断原数组中是否包含重复连续元素,包含的话则跳过;
void dfs_combinationSum2(vector<int>& candidates, int pos, int sum, int target, vector<int>&path, vector<vector<int>>& res){
if (sum >= target || pos == candidates.size()){
if (sum == target)res.push_back(path);
return;
}
if (sum > target)return;
//注释的方法和下面没有注释的方法都可以使用;
/*for (int i = pos; i < candidates.size(); i++){
path.push_back(candidates[i]);
dfs_combinationSum2(candidates, i + 1, sum + candidates[i], target, path, res);
path.pop_back();
while (i + 1 < candidates.size() && candidates[i] == candidates[i + 1])i++;
}*/
path.push_back(candidates[pos]);
dfs_combinationSum2(candidates, pos + 1, sum + candidates[pos],target, path, res);
path.pop_back();
while (pos + 1 < candidates.size() && candidates[pos] == candidates[pos + 1])pos++;
dfs_combinationSum2(candidates, pos + 1, sum, target, path, res);
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
if (candidates.size() == 0)return res;
sort(candidates.begin(), candidates.end());
vector<int> path;
dfs_combinationSum2(candidates, 0, 0, target, path, res);
return res;
}
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]]
相同的套路:
void dfs_combinationSum3(int cnt,int pos, int sum, int target, vector<int>&path, vector<vector<int>>& res){
if (cnt == 0){
if (sum == target)
res.push_back(path);
return;
}
if (sum > target || pos == 10)return;
path.push_back(pos);
dfs_combinationSum3(cnt - 1, pos + 1, sum + pos, target, path, res);
path.pop_back();
dfs_combinationSum3(cnt, pos + 1, sum, target, path, res);
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
if (k <= 0 || n <= 0)return res;
vector<int> path;
dfs_combinationSum3(k, 1, 0, n, path, res);
return res;
}
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?
int combinationSum4(vector<int>& nums, int target) {
if (nums.size() == 0)return 0;
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 1; i <= target; i++){
for (auto x : nums){
if (i >= x)dp[i] += dp[i - x];
}
}
return dp[target];
}