1. 程式人生 > >九章演算法第一課,入門

九章演算法第一課,入門

17. 子集 https://www.lintcode.com/problem/subsets/description

class Solution {
public:
    vector<vector<int>> subsets(vector<int> &nums) {
        vector<vector<int>> res;
        vector<int> sub;
        sort(nums.begin(),nums.end());
        dfshelper(nums,0,sub,res);
        return res;
    }
    void dfshelper(vector<int> &nums,int startIndex,vector<int> &sub,vector<vector<int>> &res) {
        res.push_back(sub);
        for (int i=startIndex;i<nums.size();i++) {
            sub.push_back(nums[i]);
            dfshelper(nums,i+1,sub,res);
            sub.pop_back();
        }
    }
};