-
Notifications
You must be signed in to change notification settings - Fork 0
/
216. 组合总和 III.cpp
37 lines (34 loc) · 972 Bytes
/
216. 组合总和 III.cpp
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
37
class Solution {
public:
void dfs(int k , int n , vector<int>visited,int currSum ,vector<vector<int>>&ans,vector<int>temp){
if(k<0 || currSum>n) return ;
if(k==0){
if(currSum==n){
ans.push_back(temp);
return ;
}else{
return ;
}
return ;
}
for(int i=1;i<=9;i++){
if(visited[i]==0){
visited[i]=1;
temp.push_back(i);
dfs(k-1,n,visited,currSum+i,ans,temp);
temp.pop_back();;
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>>ans;
map<vector<int>,int>mapping;;
vector<int>visited(10,0);
vector<int>temp;
dfs(k,n,visited,0,ans,temp);
for(auto i : mapping){
ans.push_back(i.first);
}
return ans;
}
};