Simple recursive solution c++ 10ms


  • 0
    H

    class Solution {
    public:

    void get_result(vector<vector<int> > &result, vector<int>s, uint i, vector<int> tmp){
        if (i==0) {
            result.push_back(tmp);
            return;
        }
        
        vector<int>a=s;
        for (uint j=0; j<=(s.size()-i);++j){
            vector<int>tmp2=tmp;
            tmp2.push_back(a.front());
            a.erase(a.begin());
            get_result(result, a, i-1, tmp2);
        }
    }
    
    vector<vector<int> > subsets(vector<int> &S) {
        sort(S.begin(), S.end());
        vector<vector<int> > result;
    
        for (uint i=0; i<=S.size(); ++i){
            vector<int> tmp;
            get_result(result, S, i, tmp);
            
        }
        return result;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.