cpp recursive simple 4ms solution


  • 0
    C
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> result;
            vector<int> subset;
            result.push_back(subset);
            helper(0, nums, subset, result);
            return result;
        }
    private:
        void helper(int begin, vector<int> & nums, vector<int> & subset, vector<vector<int>> & result) {
            for (int i = begin; i < nums.size(); i++){
                subset.push_back(nums[i]);
                result.push_back(subset);
                helper(i + 1, nums, subset, result);
                subset.pop_back();
            }
        }
    };
    

Log in to reply
 

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