class Solution { public: vector<vector<int>> res; vector<vector<int>> combine(int n, int k) { vector<int> temp; vector<int> nums; // 构建一个nums数组避免下标0数字1这些判断,避免混乱 for(int i = 1; i <= n; i++){ nums.push_back(i); } back(nums,k,0,temp); return res; } void back(vector<int> &nums, int k, int index, vector<int> &temp){ if(temp.size()==k){ res.push_back(temp); // return; } for(int i = index; i < nums.size(); i++){ temp.push_back(nums[i]); back(nums,k,i+1,temp); temp.pop_back(); // 回溯 } } };