集合的子集

题目描述

请编写一个方法,返回某集合的所有非空子集。

给定一个int数组A和数组的大小int n,请返回A的所有非空子集。保证A的元素个数小于等于20,且元素互异。各子集内部从大到小排序,子集之间字典逆序排序,见样例。

测试样例:
class Permutation {
public:
    vector<string> getPermutation(string A) {
        vector<string> res;
        if(A.size() == 0)
            return res;
        
        int cur = 0;        
        permutation(A,res,cur);
        sort(res.begin(),res.end(),greater<string>());
        
        return res;
    }
    
    void permutation(string A,vector<string> &res,int cur){
        int len = A.size();
        
        if(cur == len - 1){
            res.push_back(A);
            return;
        }
        
        for(int i = cur;i < len;i++){
            swap(A[i],A[cur]);
            permutation(A,res,cur+1);
            swap(A[i],A[cur]);
        }
    }
};

 

posted on 2017-04-20 23:56  123_123  阅读(253)  评论(0编辑  收藏  举报