class Solution {
public:
/**
*
* @param n int整型
* @param k int整型
* @return int整型vector<vector<>>
*/
vector<vector<int> > combine(int n, int k) {
// write code here
vector<vector<int>> res;
if(n<1 || k>n || k<0)
return res;
vector<int> path;
findNum(res,path,n,k,1);
return res;
}
void findNum(vector<vector<int>> &res, vector<int> &path, int n, int k, int start){
if(path.size() == k){
res.push_back(path);
}
for(int i=start; i<=n; i++){
path.push_back(i);
findNum(res, path, n, k, i+1);
path.pop_back();
}
}
};
给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合
最新推荐文章于 2025-03-05 21:24:39 发布