77. 组合

77. 组合_第1张图片

class Solution {
public:
    vector  >ans;
    vectorpath;
    void backtracking(int n,int k,int startIndex){
        if(path.size()==k){
            ans.push_back(path);
            return;
        }
        for(int i=startIndex;i<=n;i++){
            path.push_back(i);
            backtracking(n,k,i+1);
            path.pop_back();
        }
    }
    vector> combine(int n, int k) {
        backtracking(n,k,1);
        return ans;
    }
};

你可能感兴趣的:(回溯,力扣刷题,算法,leetcode,数据结构)