力扣 77. 组合


题目链接
考点:dfs
提议:1-n中取k个数,输出所有组合数的可能

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        dfs(temp,n,k,0);
        return ans;
    }

private:
    vector<vector<int>> ans;
    vector<int> temp;
    void dfs(vector<int> &temp,int n,int k,int x){
        if(temp.size()==k){
            ans.push_back(temp);
            return ;
        }
        for(int i =x+1;i<=n;i++){
            temp.push_back(i);
            dfs(temp,n,k,i);
            temp.pop_back();
        }
    }
};

你可能感兴趣的:(力扣基础题)