-
Notifications
You must be signed in to change notification settings - Fork 38
/
solve.cpp
45 lines (45 loc) · 986 Bytes
/
solve.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
44
45
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> subsets(vector<int> &nums) {
return subsets_iterative(nums);
}
private:
vector<vector<int>> subsets_iterative(vector<int> &nums) {
vector<vector<int>> result(1, vector<int>());
sort(begin(nums), end(nums));
int n = nums.size();
int m = 0;
for (int i = 0; i < n; ++i) {
int j = i > 0 && nums[i] == nums[i - 1] ? m : 0;
m = result.size();
for (; j < m; ++j) {
result.push_back(result[j]);
result.back().push_back(nums[i]);
}
}
return result;
}
};
void print(vector<vector<int>> nums)
{
for (auto v : nums) {
cout << "[";
for_each(begin(v), end(v), [](int i){cout << i << ",";});
cout << "]";
cout << endl;
}
}
int main(int argc, char **argv)
{
Solution solution;
vector<int> nums = {1,2,2};
print(solution.subsets(nums));
return 0;
}