-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0090_subsetsWithDup.cc
77 lines (72 loc) · 1.77 KB
/
Problem_0090_subsetsWithDup.cc
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
// 先排序
std::sort(nums.begin(), nums.end());
int n = nums.size();
vector<int> tmp;
vector<vector<int>> ans;
// 枚举所有子集
for (int mask = 0; mask < (1 << n); mask++)
{
tmp.clear();
bool flag = true;
for (int i = 0; i < n; i++)
{
// 检查元素是否选中
if (mask & (1 << i))
{
if (i > 0 && (mask >> (i - 1) & 1) == 0 && nums[i] == nums[i - 1])
{
// 对于 {1, 2, 2, ...}
// 观察第二个 2
// 如果前面有一个数和当前这一个数相等,前面的数没选中,当前的数选中
// 那么这时产生的结果为 {1, 2, ...}
// 与第一次遇到这个数形成的结果是一样的
// 因为第一次遇到这个 2
// 也产生了结果 {1, 2, ...}
// 因此需要过滤
flag = false;
break;
}
tmp.push_back(nums[i]);
}
}
if (flag)
{
ans.push_back(tmp);
}
}
return ans;
}
};
void test()
{
Solution s;
vector<int> n1 = {1, 2, 2};
vector<int> n2 = {0};
auto x1 = s.subsetsWithDup(n1);
auto x2 = s.subsetsWithDup(n2);
vector<vector<int>> o1 = {{}, {1}, {1, 2}, {1, 2, 2}, {2}, {2, 2}};
vector<vector<int>> o2 = {{}, {0}};
std::sort(x1.begin(), x1.end());
std::sort(x2.begin(), x2.end());
std::sort(o1.begin(), o1.end());
std::sort(o2.begin(), o2.end());
EXPECT_TRUE(o1 == x1);
// EXPECT_TRUE(o2 == x2);
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}