-
Notifications
You must be signed in to change notification settings - Fork 0
/
2215_find_the_difference_of_two_arrays.cpp
48 lines (40 loc) · 1.69 KB
/
2215_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
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
class Solution{
public:
std::vector<std::vector<int>> findDifference(std::vector<int>& nums1, std::vector<int>& nums2){
std::set<int> answer1;
std::set<int> answer2;
std::set<int> set_nums1(nums1.begin(), nums1.end());
std::set<int> set_nums2(nums2.begin(), nums2.end());
for (int num : set_nums1){
if (set_nums2.find(num) == set_nums2.end()){
answer1.insert(num);
}
}
for (int num : set_nums2){
if (set_nums1.find(num) == set_nums1.end()){
answer2.insert(num);
}
}
return {std::vector<int>(answer1.begin(), answer1.end()), std::vector<int>(answer2.begin(), answer2.end())};
}
};
int main()
{
Solution s;
// Case 1
std::vector<int> nums1_case1 = {1, 2, 3};
std::vector<int> nums2_case1 = {2, 4, 6};
std::vector<std::vector<int>> result1 = s.findDifference(nums1_case1, nums2_case1);
std::vector<std::vector<int>> expected1 = {{1, 3}, {4, 6}};
std::cout << "Result: {{" << result1[0][0] << "," << result1[0][1] << "},{" << result1[1][0] << "," << result1[1][1] << "}}, Expected: {{" << expected1[0][0] << "," << expected1[0][1] << "},{" << expected1[1][0] << "," << expected1[1][1] << "}}\n";
std::vector<int> nums1_case2 = {1, 2, 3, 3};
std::vector<int> nums2_case2 = {1, 1, 2, 2};
std::vector<std::vector<int>> result2 = s.findDifference(nums1_case2, nums2_case2);
std::vector<std::vector<int>> expected2 = {{3}, {}};
std::cout << "Result: {{" << result2[0][0] << "},{}}, Expected: {{" << expected2[0][0] << "},{}}\n";
return 0;
}