My recursive cpp code


  • 0
    C
    class Solution {
    public:
    
        vector< vector<int> > dfs(vector<int> nums)
        {
            if( !nums.size() ) return vector< vector<int> >( 1, vector<int>() );
            
            vector< vector<int> > result = dfs( vector<int>(nums.begin(), nums.end()-1) );
            
            int result_presize = result.size();
            for(int i=0; i < result_presize; i++)
            {
                vector<int> subset = result[i];
                subset.push_back( nums.back() );
                result.push_back(subset);
            }
            
            return result;
        }
    
        vector< vector<int> > subsets(vector<int>& nums) 
        {
            std::sort(nums.begin(), nums.end());
            return dfs(nums);
        }
    };

  • 0
    G

    What is its runtime?


Log in to reply
 

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