90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- 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], [] ]
class Solution { public: void dfs(vector<vector<int>>&u,int x,vector<int>& nums,vector<int>&v,int cnt,int n){ if(cnt==n){ //n++; u.push_back(v); return ; } for(int i=x;i<nums.size();i++){ if(nums[i]==nums[i-1]&&i>x)continue; v.push_back(nums[i]); dfs(u,i+1,nums,v,cnt+1,n); v.pop_back(); } } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>>u; vector<int>v; sort(nums.begin(),nums.end()); for(int i=0;i<=nums.size();i++){ dfs(u,0,nums,v,0,i); } return u; } };
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: