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