Leetcode90. Subsets II子集2

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

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

示例:

输入: [1,2,2] 输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]

class Solution {
public:
    int len;
    vector<vector<int> > res;
    vector<vector<int> > subsetsWithDup(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        len = nums.size();
        vector<int> temp;
        DFS(nums, temp, 0);
        return res;
    }

    void DFS(vector<int>& nums, vector<int> temp, int pos)
    {
        res.push_back(temp);
        for(int i = pos; i < len; i++)
        {
            temp.push_back(nums[i]);
            DFS(nums, temp, i + 1);
            temp.pop_back();
            for(; i < len - 1; i++)
            {
                if(nums[i + 1] != nums[i])
                {
                    break;
                }
            }
        }
    }
};
原文地址:https://www.cnblogs.com/lMonster81/p/10433851.html