-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_out_of_three.cpp
More file actions
58 lines (54 loc) · 1.49 KB
/
two_out_of_three.cpp
File metadata and controls
58 lines (54 loc) · 1.49 KB
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
/*
* =====================================================================================
*
* Filename: two_out_of_three.cpp
*
* Description: 2032. Two Out of Three.
* https://leetcode.com/problems/two-out-of-three
*
* Version: 1.0
* Created: 10/10/2021 22:30:20
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <map>
#include <set>
#include <vector>
class Solution {
public:
std::vector<int> twoOutOfThree(std::vector<int>& nums1, std::vector<int>& nums2,
std::vector<int>& nums3) {
std::map<int, int> nums_map;
auto insert = [&](const std::vector<int>& nums) {
const std::set<int> s(nums.begin(), nums.end());
for (const int v : s) {
nums_map[v]++;
}
};
insert(nums1);
insert(nums2);
insert(nums3);
std::vector<int> vals;
for (const auto e : nums_map) {
if (e.second > 1) {
vals.push_back(e.first);
}
}
return vals;
}
};
int main(int argc, char* argv[]) {
std::vector<int> nums1 = {1, 1, 3, 2};
std::vector<int> nums2 = {2, 3, 2};
std::vector<int> nums3 = {3};
std::vector<int> vals = Solution().twoOutOfThree(nums1, nums2, nums3);
std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}