//这是个比较好的回溯模板
class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int>> res; vector<int> add; DFS(res,add,k,n,1); return res; } void DFS(vector<vector<int>>& res,vector<int> add,int k,int n,int pos){ if(n < 0) return; if(n == 0&&add.size() == k){ res.push_back(add); } else{ for(int i=pos;i <= 9;i++){ add.push_back(i); DFS(res,add,k,n-i,i+1); add.pop_back(); } } } };