回溯Leetcode 77 Combinations

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shey666/article/details/80792541

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

猜你喜欢

转载自blog.csdn.net/shey666/article/details/80792541
今日推荐