题目链接:
https://leetcode-cn.com/problems/combination-sum-iii/
难度:中等
216. 组合总和 III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
说明:
所有数字都是正整数。
解集不能包含重复的组合。
示例 1:
输入: k = 3, n = 7
输出: [[1,2,4]]
示例 2:
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
I barely made it. I wrote some rubbish, and I went the wrong way (code rubbish recorded as a reminder).
At the beginning of the for loop, I did not expect it. If it is not used, the final result will be repeated. (Isn't this the idea of yesterday's question... But yesterday's state was not good)
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> tmp;
dfs(k,1,n,tmp);
return res;
}
void dfs(int k,int pos,int rest,vector<int> &tmp){
int num=tmp.size();
if(num==k||rest<=0){
if(num==k&&!rest){
res.push_back(tmp);
}
return;
}
for(int i=pos;i<=9;i++){
tmp.push_back(i);
dfs(k,i+1,rest-i,tmp);
tmp.pop_back();
}
}
};
The rest of the idea is to solve the problem (the first method does not feel very good)
This is to consider all the situations again. Note: all the situations of the ninth power of two are judged.
class Solution {
public:
vector<vector<int>> res;
vector<int> temp;
vector<vector<int>> combinationSum3(int k, int n) {
// i<(1<<9=512) 0-511 共512种情况 对应的二进制位
// 1代表选择 0代表不选
// (1<<i)&mash 判断哪个位置是1
// 在push的时候要(i+1) (对应数字是1-9 而位置是0-8)
// 当然这种情况肯定是不会有冲突的
for(int i=0;i<(1<<9);++i){
if(check(i,k,n)){
res.push_back(temp);
}
}
return res;
}
bool check(int mash,int k,int n){
temp.clear();
for(int i=0;i<9;++i){
if((1<<i)&mash){
temp.push_back(i+1);
}
}
// 保证共有k个
// accumulate第一次看见 求和的
return temp.size()==k&&accumulate(temp.begin(),temp.end(),0)==n;
}
};
There is a better way to look more beautiful
class Solution {
public:
vector<vector<int>> res;
vector<int> temp;
vector<vector<int>> combinationSum3(int k, int n) {
dfs(1,9,k,n);
return res;
}
// cur当前的数值
// n=9 没有也没事 直接把n改成9也行。。。
// k 记录需要几个数字
// sum相当于combinationSum3(k,n) 中的n 需要的和
void dfs(int cur,int n,int k,int sum){
// 到不了k个 或者 超过k个 结束
if (temp.size()+(n-cur+1)<k||temp.size()>k) {
return;
}
if(temp.size()==k&&accumulate(temp.begin(),temp.end(),0)==sum){
res.push_back(temp);
return;
}
temp.push_back(cur);
dfs(cur+1,n,k,sum);
temp.pop_back();
dfs(cur+1,n,k,sum);
}
};