LeetCode 78 - 子集

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
 [3],
 [1],
 [2],
 [1,2,3],
 [1,3],
 [2,3],
 [1,2],
 []
]

 

class Solution
{
public:
    vector<vector<int>> subsets(const vector<int>& nums)
    {
        vector<vector<int>> res;
        vector<int> p;
        res.push_back(p);
        for(auto x:nums)
        {
            int sz=res.size();
            for(int i=0;i<sz;i++)
            {
                p=res[i];
                p.push_back(x);
                res.push_back(p);
            }
        }
        return res;
    }
};

 

posted @ 2019-04-25 09:42  Dilthey  阅读(108)  评论(0编辑  收藏  举报