Skip to content

Commit 6651a7b

Browse files
committed
2
1 parent e4bc727 commit 6651a7b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

SubsetsII/SubsetsII.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> subsetsWithDup(vector<int>& S) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
8+
vector<vector<int>> subsets;
9+
vector<int> integers;
10+
11+
if (S.empty()) return subsets;
12+
13+
sort(S.begin(), S.end());
14+
DFS(integers, 0, S, subsets);
15+
16+
return move(subsets);
17+
}
18+
19+
void DFS(vector<int>& integers, int start, vector<int>& S, vector<vector<int>>& subsets) {
20+
if (start == S.size()) {
21+
subsets.push_back(integers);
22+
return;
23+
}
24+
int end = start;
25+
while (end + 1 < S.size() && S[start] == S[end+1])
26+
end += 1;
27+
28+
DFS(integers, end + 1, S, subsets);
29+
30+
for (int i = start; i <= end; i++) {
31+
integers.push_back(S[i]);
32+
DFS(integers, end + 1, S, subsets);
33+
}
34+
for (int i = start; i <= end; i++)
35+
integers.pop_back();
36+
}
37+
};

0 commit comments

Comments
 (0)