forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.cpp
More file actions
23 lines (23 loc) · 727 Bytes
/
s3.cpp
File metadata and controls
23 lines (23 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// OJ: https://leetcode.com/problems/subsets-ii/
// Author: github.com/lzl124631x
// Time: O(N^2 * 2^N)
// Space: O(N)
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans(1);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ) {
int cnt = 0, n = nums[i], len = ans.size();
while (i < nums.size() && nums[i] == n) ++cnt, ++i;
for (int j = 0; j < len; ++j) {
vector<int> sub = ans[j];
for (int k = 0; k < cnt; ++k) {
sub.push_back(n);
ans.push_back(sub);
}
}
}
return ans;
}
};