-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCProblem90.cpp
43 lines (40 loc) · 1.32 KB
/
LCProblem90.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
90. Subsets II
Medium
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.
Passes - Beats 53%
*/
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
// Set_size of power set of a set with set_size
// n is (2^n-1)
unsigned int pow_set_size = pow(2, nums.size());
set<vector<int>> mySet;
vector<int> tmp;
mySet.insert(tmp);
// Iterate from 000..0 to 111..1
for (int i = 0; i < pow_set_size; i++) {
for (int j = 0; j < nums.size(); j++) {
// Check if jth bit in the counter is set
// If set then add the jth element from set to the vector
// sort, and add it to the set power set
// we sort the vector so that values like [1,4,4] and [4,4,1]
// get caught
if (i & (1 << j)) {
tmp.push_back(nums[j]);
sort(tmp.begin(), tmp.end());
mySet.insert(tmp);
}
}
// clear the array to get new values
tmp.clear();
}
// convert the set to a vector
vector<vector<int>> rv(mySet.begin(), mySet.end());
return rv;
}
};