forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0090.cpp
More file actions
executable file
·28 lines (26 loc) · 741 Bytes
/
LC0090.cpp
File metadata and controls
executable file
·28 lines (26 loc) · 741 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/subsets-ii/
Time: O(n • 2ⁿ)
Space: O(n • 2ⁿ)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<int> subset;
vector<vector<int>> subsets;
sort(nums.begin(), nums.end());
generate_subset(0, nums, subset, subsets);
return subsets;
}
void generate_subset(int pos, vector<int>& nums, vector<int>& subset, vector<vector<int>>& subsets) {
subsets.push_back(subset);
for (int i = pos; i < nums.size(); i++) {
if (i > pos && nums[i - 1] == nums[i])
continue;
subset.push_back(nums[i]);
generate_subset(i + 1, nums, subset, subsets);
subset.pop_back();
}
}
};