-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0090.cpp
More file actions
50 lines (46 loc) · 1.25 KB
/
0090.cpp
File metadata and controls
50 lines (46 loc) · 1.25 KB
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
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums) { return func1(nums); }
vector<vector<int>> func1(vector<int> &nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ret(1);
int curr = 0, next = 0;
while (curr < nums.size()) {
vector<vector<int>> aux = mycopy(ret);
aux = helper1(nums, &curr, &next, aux);
for (int i = 0; i < aux.size(); i++) {
ret.emplace_back(aux[i]);
}
curr = next;
}
return ret;
}
vector<vector<int>> mycopy(vector<vector<int>> &arr) {
vector<vector<int>> aux;
for (auto a : arr) {
vector<int> row(a.begin(), a.end());
aux.emplace_back(row);
}
return aux;
}
vector<vector<int>> helper1(vector<int> &nums, int *curr, int *next,
vector<vector<int>> &arr) {
vector<vector<int>> ret;
int lo = *curr, hi = *curr;
while (hi < nums.size() && nums[hi] == nums[lo]) {
hi++;
}
*next = hi;
hi--;
for (int mid = lo; mid <= hi; mid++) {
for (auto a : arr) {
vector<int> aux(a.begin(), a.end());
for (int i = lo; i <= mid; i++) {
aux.emplace_back(nums[i]);
}
ret.emplace_back(aux);
}
}
return ret;
}
};