-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode2215-find-the-difference-of-two-arrays.cpp
93 lines (72 loc) · 1.93 KB
/
leetcode2215-find-the-difference-of-two-arrays.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* leetcode2215-find-the-difference-of-two-arrays.cpp
*
* @author Wang Guibao (wang_guibao@163.com)
* @date 2023/10/09 16:23
* @brief https://leetcode.com/problems/find-the-difference-of-two-arrays
*/
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
std::vector<std::vector<int>> retVec;
std::set<int> s1;
std::set<int> s2;
for (auto x: nums1) {
s1.insert(x);
}
for (auto x: nums2) {
s2.insert(x);
}
std::vector<int> vec;
for (auto x: s1) {
if (s2.find(x) == s2.end()) {
vec.push_back(x);
}
}
retVec.push_back(vec);
vec.clear();
for (auto x: s2) {
if (s1.find(x) == s1.end()) {
vec.push_back(x);
}
}
retVec.push_back(vec);
return retVec;
}
};
int main() {
while (1) {
std::vector<int> nums1;
std::vector<int> nums2;
int n;
std::cout << "Input number of interges in nums1: ";
std::cin >> n;
std::cout << "Input " << n << " integers for nums1: ";
for (int i = 0; i < n; ++i) {
int x;
std::cin >> x;
nums1.push_back(x);
}
std::cout << "Input number of interges in nums2: ";
std::cin >> n;
std::cout << "Input " << n << " integers for nums2: ";
for (int i = 0; i < n; ++i) {
int x;
std::cin >> x;
nums2.push_back(x);
}
Solution solution;
std::vector<std::vector<int>> ret = solution.findDifference(nums1, nums2);
for (auto v : ret) {
for (auto x: v) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
}
return 0;
}