-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path216.cpp
More file actions
33 lines (31 loc) · 774 Bytes
/
216.cpp
File metadata and controls
33 lines (31 loc) · 774 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
//
// 216.cpp
// leetcode
//
// Created by R Z on 2018/3/30.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
vector<int> tmp;
backtrack(res, tmp, k, n, 1);
return res;
}
void backtrack(vector<vector<int>> &res, vector<int> &temp, int k, int remain, int start){
if(k==0 && remain==0){
res.push_back(temp);
return;
}else{
for(int i=start;i<10;i++){
temp.push_back(i);
backtrack(res, temp,k-1,remain-i,i+1);
temp.pop_back();
}
}
}
};