C++ 8ms dfs solution. clean code


  • 0
    void dfs(vector<vector<int>> &res, vector<int> &pre, int i,vector<int> & nums){
        if (i>=nums.size()) {res.push_back(pre);return;}
        dfs(res,pre,i+1,nums);
        pre.push_back(nums[i]);
        dfs(res,pre,i+1,nums);
        pre.pop_back();
    }
    
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> pre;
        dfs(res,pre,0,nums);
        return res;
    }

Log in to reply
 

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