-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetsII.cpp
More file actions
26 lines (25 loc) · 907 Bytes
/
subsetsII.cpp
File metadata and controls
26 lines (25 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] */
class Solution {
private:
void findSubsets(int ind, vector<int> &nums, vector<int> &ds, vector<vector<int>> &ans) {
ans.push_back(ds);
for(int i = ind;i<nums.size();i++) {
if(i!=ind && nums[i] == nums[i-1]) continue;
ds.push_back(nums[i]);
findSubsets(i+1, nums, ds, ans);
ds.pop_back();
}
}
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> ds;
sort(nums.begin(), nums.end());
findSubsets(0, nums, ds, ans);
return ans;
}
};