标签:
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
1 class Solution { 2 public: 3 vector<vector<int>> subsetsWithDup(vector<int>& nums) { 4 vector<vector<int>> res; 5 res.push_back(vector<int> (0,0)); 6 7 vector<int> tmp; 8 sort(nums.begin(),nums.end()); 9 sub(nums,0,tmp,res); 10 return res; 11 12 } 13 void sub(vector<int> &nums,int s,vector<int> &tmp,vector<vector<int>> &res) 14 { 15 int len=nums.size(); 16 for(int i=s;i<len;i++) 17 { 18 if(i>s&&nums[i]==nums[i-1]) 19 continue; 20 tmp.push_back(nums[i]); 21 res.push_back(tmp); 22 sub(nums,i+1,tmp,res); 23 tmp.pop_back(); 24 } 25 } 26 27 28 };
标签:
原文地址:http://www.cnblogs.com/hexhxy/p/5567260.html