-
Notifications
You must be signed in to change notification settings - Fork 1
/
Subsets.cpp
78 lines (68 loc) · 2.3 KB
/
Subsets.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Generate subsets
// Problem - https://leetcode.com/problems/subsets/
// Problem - https://leetcode.com/problems/subsets-ii/
// Discussion
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/795/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning)
#include <iostream>
#include <vector>
#include <unordered_set>
void backtrack(std::vector<std::vector<int>>& sets, const std::vector<int>& nums,
int start, std::vector<int>& current)
{
sets.push_back(current);
for (int i = start; i < nums.size(); ++i) {
current.push_back(nums[i]);
backtrack(sets, nums, i + 1, current);
current.pop_back();
}
}
std::vector<std::vector<int>> subsets(const std::vector<int>& nums)
{
std::vector<std::vector<int>> sets;
std::vector<int> temp;
backtrack(sets, nums, 0, temp);
return sets;
}
void backtrackUnique(std::vector<std::vector<int>>& sets, const std::vector<int>& nums,
int start, std::vector<int>& current, std::unordered_set<int>& processed)
{
sets.push_back(current);
for (int i = start; i < nums.size(); ++i) {
if (processed.find(nums[i]) != processed.end()) {
continue;
}
current.push_back(nums[i]);
processed.emplace(nums[start]);
backtrackUnique(sets, nums, i + 1, current, processed);
processed.erase(nums[start]);
current.pop_back();
}
}
std::vector<std::vector<int>> subsetsUnique(const std::vector<int>& nums)
{
std::vector<std::vector<int>> sets;
std::vector<int> temp;
std::unordered_set<int> processed;
backtrackUnique(sets, nums, 0, temp, processed);
return sets;
}
void print(std::vector<std::vector<int>> sets)
{
for (auto set : sets) {
for (auto s : set) {
std::cout << s << " ";
}
std::cout << "\n";
}
}
int main()
{
std::vector<int> nums {1, 2, 3, 3};
std::cout << "==========================================\n";
std::vector<std::vector<int>> setsWithDuplicates = subsets(nums);
print(setsWithDuplicates);
std::cout << "==========================================\n";
std::vector<std::vector<int>> setsWithoutDuplicates = subsetsUnique(nums);
print(setsWithoutDuplicates);
return 0;
}