"Elements in a subset must be in non-descending order.",so must be sorted first, my c++ code.


  • 0
    L

    class Solution {
    public:
    vector<vector<int>> re;
    vector<int> t;

    void subs(vector<int> &S,int depth,int maxD,int s)
    {
        if (depth == maxD)
        {
            re.push_back(t);
            return;
        }
        for (int i = s;i<S.size();i++)
        {
            t[depth] = S[i];
            subs(S,depth+1,maxD,i+1);
        }
    }
    vector<vector<int> > subsets(vector<int> &S) {
        re.clear();
        re.push_back(t);
        sort(S.begin(),S.end()); //must be sorted
        for (int i = 0;i<S.size();i++)
        {
            t.resize(i+1);
            subs(S,0,i+1,0);
        }
        return re;
    }
    

    };


Log in to reply
 

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