My c++ solution, 8ms, easy to understand


  • 0
    W
    class Solution {
        private: vector<vector<int> >res;
                 vector<int>a;
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            if(nums.empty())
            return res;
            a.resize(nums.size());
            sort(nums.begin(),nums.end());
            solve(0,nums.size(),nums);
            return res;
        }
        void solve(int dep, int maxDep, vector<int> &cand)
        {
            if (dep == maxDep)
            {
                    vector<int> ret;
                    for(int i = 0; i < maxDep; i++)
                        for(int j = 0; j < a[i]; j++)
                            ret.push_back(cand[i]);
                    res.push_back(ret);
                return;
            }
            
            for(int i = 0; i <= 1; i++)
            {
                a[dep] = i;
                solve(dep + 1,maxDep,cand);
            }
        }
    };

Log in to reply
 

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