LeetCode77. 组合

题目大意:求出1~n中所有可能的k个数的组合。

题目分析:本题使用递归求解比较简单,当固定第一个数“1”时,就相当于求解后面2~n中所有可能的k-1组合,然后一直到k等于0,表示一种组合情况已经得到,保存到res中即可。

代码展示:

class Solution {
public:
    vector> combine(int n, int k) {
        vector> res;
        vector temp;
        if(n &temp,vector> &res){
        if(k==0){
            res.push_back(temp);
            return;
        }
        for(int i=begin;i<=end;i++){
            temp.push_back(i);
            combineSolve(i+1,end,k-1,temp,res);
            temp.pop_back();
        }
    }
};

 

你可能感兴趣的:(C++,递归,LeetCode,LeetCode)