1. 程式人生 > >回溯Leetcode 77 Combinations

回溯Leetcode 77 Combinations

Leetcode 77

Combinations

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); } } } };