给定一个可能包含重复整数的列表,返回所有可能的子集(幂集)。
注意事项:解决方案集不能包含重复的子集。
例如,如果 nums = [1,2,2],答案为:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
详见:https://leetcode.com/problems/subsets-ii/description/
class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> res; vector<int> out; sort(nums.begin(),nums.end()); helper(nums,0,out,res); return res; } void helper(vector<int> &nums,int start,vector<int> &out,vector<vector<int>> &res) { res.push_back(out); for(int i=start;i<nums.size();++i) { out.push_back(nums[i]); helper(nums,i+1,out,res); out.pop_back(); while(i+1<nums.size()&&nums[i+1]==nums[i]) { ++i; } } } };
参考:https://www.cnblogs.com/grandyang/p/4310964.html