版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shey666/article/details/80792541
Leetcode 77
Combinations
- Problem Description:
经典回溯问题:给出n个数字,要求返回其中k个数的组合情况。
具体的题目信息:
https://leetcode.com/problems/combinations/description/ - Example:
- Solution:
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> t;
combination(1, k, n, t, res);
return res;
}
void combination(int i, int k, int n, vector<int>& t, vector<vector<int>>& res) {
if (k == t.size()) {
res.push_back(t);
return;
} else {
for (int j = i; j <= n; j++) {
t.push_back(j);
combination(j+1, k, n, t, res);
t.erase(t.begin()+t.size()-1);
}
}
}
};