- class Solution {
- public:
- vector
int> >ans; - vector<int>path;
- void backtracking(int n,int k,int sum,int star){
- if(path.size()==k){
- if(sum==n)ans.push_back(path);
- return;
- }
- for(int i=star;i<=9;i++){
- sum+=i;
- path.push_back(i);
- backtracking(n,k,sum,i+1);
- sum-=i;
- path.pop_back();
- }
- }
- vector
int>> combinationSum3(int k, int n) { - backtracking(n,k,0,1);
- return ans;
- }
- };