https://leetcode-cn.com/problems/combinations/submissions/(组合)
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans;
vector<int> track;
bt(n,1,k,track,ans);
return ans;
}
void bt(int n, int start, int k, vector<int> &track, vector<vector<int>> &ans)
{
if (track.size() == k)
{
ans.push_back(track);
return;
}
// 这句return是优化
if (track.size()+n-start+1<k) return;
for (int i = start; i <= n; ++i)
{
track.push_back(i);
bt(n,i+1,k,track,ans);
track.pop_back();
}
}
};
https://leetcode-cn.com/problems/combination-sum/(无重复)
class Solution {
private:
vector<int> candidates;
vector<vector<int>> res;
vector<int> path;
public:
void DFS(int start, int target) {
if (target == 0) {
res.push_back(path);
return;
}
for (int i = start;
i < candidates.size() && target - candidates[i] >= 0; i++) {
path.push_back(candidates[i]);
DFS(i, target - candidates[i]);
path.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int> &candidates, int target) {
std::sort(candidates.begin(), candidates.end());
this->candidates = candidates;
DFS(0, target);
return res;
}
};
https://leetcode-cn.com/problems/combination-sum-ii/ (有重复)
class Solution {
private:
vector<int> candidates;
vector<vector<int>> res;
vector<int> path;
public:
void DFS(int start, int target) {
if (target == 0) {
res.push_back(path);
return;
}
for (int i = start; i < candidates.size() && target - candidates[i] >= 0; i++) {
if (i > start && candidates[i] == candidates[i - 1])
continue;
path.push_back(candidates[i]);
DFS(i+1, target - candidates[i]);
path.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
std::sort(candidates.begin(), candidates.end());
this->candidates = candidates;
DFS(0, target);
return res;
}
};
https://leetcode-cn.com/problems/combination-sum-iii/
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ans;
vector<int> track;
bt(k,n,0,1,track,ans);
return ans;
}
void bt(int k, int n, int sum, int start, vector<int> &track, vector<vector<int>> &ans)
{
if (track.size() == k)
{
if (n==sum)
{
ans.push_back(track);
}
return;
}
if (sum >= n)
return;
for (int i = start; i <= 9; ++i)
{
if (sum+i<=n)
{
track.push_back(i);
bt(k,n,sum+i,i+1,track,ans);
track.pop_back();
}
}
}
};
https://leetcode-cn.com/problems/combination-sum-iv/
//记忆化递归
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> memo(target+1, -1);
return helper(nums, target, memo);
}
int helper(vector<int>& nums, int target, vector<int> &memo) {
if (target < 0) return 0;
if (target == 0) return 1;
if (memo[target] != -1) return memo[target];
int ans = 0;
for(int i = 0; i < nums.size(); ++i)
{
ans += helper(nums, target-nums[i], memo);
}
memo[target] = ans;
return ans;
}
};
//动态规划
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
long dp[target+1] = {};
dp[0] = 1;
for (int i = 1; i <= target; ++i)
{
//printf("%d\n", i);
for (int j = 0; j < nums.size(); ++j)
{
int t = i - nums[j];
if (t>=0)
{
if (dp[i]>INT_MAX-dp[t]) //需要注意溢出问题
{
break;
}
dp[i] += dp[t];
}
}
}
return dp[target];
}
};