Simple C++ solution


  • 1
    G

    class Solution {
    public:
    vector<vector<int>> result;

    void returnallSubsets(vector<int> nums,vector<int> temp, int len)
    {
        // len keeps increasing but the elements in temp are subsets
        if(len == nums.size())
        {
            vector<int> newVect(temp.begin(),temp.end());
            sort(newVect.begin(),newVect.end());
            result.push_back(newVect);
        }
        else
        {
            // either the number should not be included
            returnallSubsets(nums,temp,len+1);
            temp.push_back(nums[len]);
            // or it should be 
            returnallSubsets(nums,temp,len+1);
        }
    }
    
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> temp;
        returnallSubsets(nums,temp,0);
        return result;
    }
    

    };


Log in to reply
 

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