标签:lin under color track blog == pop ++ backtrack
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],
[]
]
思路:
与不含重复元素相比,只是添加了一句代码验证一下。
void getsubset(vector<vector<int>>& subs,vector<int>& sub,vector<int>& nums,int start) { subs.push_back(sub); for(int i= start;i<nums.size();i++) { if(i==start || nums[i] != nums[i-1]) { sub.push_back(nums[i]); getsubset(subs,sub,nums,i+1); sub.pop_back(); } } } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> subs; vector<int>sub; sort(nums.begin(),nums.end()); getsubset(subs,sub,nums,0); return subs; }
参考:
标签:lin under color track blog == pop ++ backtrack
原文地址:http://www.cnblogs.com/hellowooorld/p/6938909.html