题目描述:
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
AC C++ Solution:
回溯法:
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> >res;
if(n<k)return res;
vector<int> temp(0,k);
combine(res,temp,0,0,n,k);
return res;
}
void combine(vector<vector<int> > &res,vector<int> &temp,int start,int num,int n ,int k){
if(num==k){
res.push_back(temp);
return;
}
for(int i = start;i<n;i++){
temp.push_back(i+1);
combine(res,temp,i+1,num+1,n,k);
temp.pop_back();
}
}
};