Combinations

/*
题意:给两个数n,和k,求1-n的组合情况
解法:递归求解
*/
class Solution {
public:
    vector<vector<int> >res;
    int count;
    void Comb(int A[],int n,int k,int step){
        if(step == k){//找到一种情况
            res.push_back(vector<int>());
            for(int i = 0 ; i < k ; i++){
                res[count].push_back(A[i]);
            }
            count++;
            return ;
        }
        for(int i = (step == 0)?1:A[step-1]+1; i<=n ; i++){
                A[step] = i;
                Comb(A,n,k,step+1);
        }
    }
    vector<vector<int> > combine(int n, int k) {
        int A[k+1];
        count = 0;
        Comb(A,n,k,0);
        return res;
    }
};

 

posted @ 2015-04-16 17:35  SprayT  阅读(115)  评论(0编辑  收藏  举报