-
Notifications
You must be signed in to change notification settings - Fork 0
/
2032. Two Out of Three 3.cpp
52 lines (41 loc) · 1.11 KB
/
2032. Two Out of Three 3.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
// https://leetcode.com/problems/two-out-of-three/
class Solution
{
vector<int> result;
unordered_set<int> nums;
unordered_set<int> numbers1;
unordered_set<int> numbers2;
unordered_set<int> numbers3;
public:
vector<int> twoOutOfThree(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3)
{
Insert(nums, nums1);
Insert(nums, nums2);
Insert(nums, nums3);
Insert(numbers1, nums1);
Insert(numbers2, nums2);
Insert(numbers3, nums3);
Filter();
return result;
}
void Filter()
{
for (const auto &num : nums)
{
int counter = 0;
if (numbers1.find(num) != numbers1.end())
counter++;
if (numbers2.find(num) != numbers2.end())
counter++;
if (numbers3.find(num) != numbers3.end())
counter++;
if (counter >= 2)
result.push_back(num);
}
}
void Insert(unordered_set<int> &nums, vector<int> &givenNums)
{
for (int &num : givenNums)
nums.insert(num);
}
};