题解 | #牛棚分组#
牛棚分组
https://www.nowcoder.com/practice/d5740e4dde3740828491236c53737af4
#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @param k int整型
* @return int整型vector<vector<>>
*/
vector<vector<int>> res;
vector<int> path;
bool st[20]={false};
void dfs(int u,int n,int k){
if(path.size()==k){
res.push_back(path);
return;
}
for(int i=u;i<=n;i++){
if(!st[i]){
st[i]=true;
path.push_back(i);
dfs(i+1,n,k);
path.pop_back();
st[i]=false;
}
}
}
vector<vector<int> > combine(int n, int k) {
dfs(1, n, k);
return res;
}
};
