1. 程式人生 > >leetcode 78:子集

leetcode 78:子集

void push(std::vector<std::vector<int>> &a,std::vector<int> &v,std::vector<int> &nums,int n,int s){
    if(v.size()==n){
        a.push_back(v);
        return;
    }
    for(int i=s;i<nums.size();i++){
        v.push_back(nums[i]);
        push(a,v,nums,n,i+1);
        v.pop_back();
    }
}

std::vector<std::vector<int>> subsets(std::vector<int>& nums) {
    int n=0;
    std::vector<int> v;
    std::vector<std::vector<int>> a;
    std::vector<int> flag;
    for(int i=0;i< nums.size();i++){
        flag.push_back(0);
    }
    while(n!=nums.size()+1){
        v.clear();
        if(n==0)
            a.push_back(v);
        else{
            push(a,v,nums,n,0);
        }
        n++;
    }
    return a;
}