-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path216.txt
More file actions
36 lines (31 loc) · 822 Bytes
/
216.txt
File metadata and controls
36 lines (31 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
unordered_set<int>hash;
int k,n;
vector<vector<int>>res;
vector<vector<int>> combinationSum3(int k, int n) {
this->k=k;
this->n=n;
vector<int>cur;
dfs(cur,0,0);
return res;
}
void dfs(vector<int>&cur,int sum,int state){
if(sum>n)return;
if(cur.size()==k){
if(sum==n){
if(hash.find(state)==hash.end()){
res.push_back(cur);
hash.insert(state);
}
}
return;
}
for(int i=1;i<=9;i++){
if((state&(1<<i))!=0)continue;
cur.push_back(i);
dfs(cur,sum+i,state|(1<<i));
cur.pop_back();
}
}
};