LetCode 77. 组合

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    vector> combine(int n, int k) {
        vector nums(n + 1, 0);
        vector> res;
        vector tmp;
        solve(1, tmp, res, nums, k);
        return res;
    } 
    
    void solve(int index, vector& tmp, vector>& res, vector& nums, int k){
        //当已经选取k个数后输出
        if (tmp.size() == k){
            res.push_back(tmp);
            return;
        }        
        if (nums.size() - index + 1 < k - tmp.size())
            return;
        for (int i = index; i < nums.size(); i++){
            tmp.push_back(i);
            solve(i + 1, tmp, res, nums, k);
            tmp.pop_back();
        }
        return;
    }
};

你可能感兴趣的:(LetCode)