My simple C++ solution


  • 0
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> ret;
            if(nums.size()==0)  {
                ret.push_back(vector<int>{});
                return ret;
            } 
            ret.push_back(vector<int>{nums[0]});
            for(int i=1;i<nums.size();i++){
                vector<vector<int>> temp;
                temp.push_back(vector<int>{nums[i]});
                for(auto x:ret){
                    x.push_back(nums[i]);
                    temp.push_back(x);
                }
                ret.insert(ret.end(),temp.begin(),temp.end());
            }
           ret.push_back(vector<int>{});
           return ret;
        }
    };
    

Log in to reply
 

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