clean c++ solution beats 90%


  • 0
    D
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> ans;
            vector<int> chosen;
            ans.push_back(chosen);
            subsets_inner(nums, ans, chosen, 0);
            return ans;        
        }
        void subsets_inner(vector<int> &nums, vector<vector<int>> &ans, 
                    vector<int> &chosen, int start){
            for (int i = start; i < nums.size(); i++){
                chosen.push_back(nums[i]);
                ans.push_back(chosen);
                if (i + 1 < nums.size())
                    subsets_inner(nums, ans, chosen, i + 1);
                chosen.pop_back();
            }
        }
    };
    

Log in to reply
 

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