8.4 Power Set

Simple backtracking question.

    void getSubsets(vector<vector<int>>& result, vector<int>& nums, vector<int>& sub,int pos){
        result.push_back(sub);
        for (int i=pos; i<nums.size(); ++i) {
            sub.push_back(nums[i]);
            getSubsets(result, nums, sub, i+1);
            sub.pop_back();
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> result;
        vector<int> sub;
        getSubsets(result, nums, sub, 0);
        return result;
    }

你可能感兴趣的:(Backtrack)