add: 230503

This commit is contained in:
Eatswap 2023-05-03 12:56:57 +08:00
parent 78365bc52a
commit bfa1062df6
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 47 additions and 0 deletions

36
cpp/2305/LC230503.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <algorithm>
#include <iterator>
#include <vector>
#include <unordered_set>
/**
* 2215. Find the Difference of Two Arrays
*
* Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
* answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
* answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
* Note that the integers in the lists may be returned in any order.
*/
class LC230503 {
public:
template<typename T>
using V = std::vector<T>;
using VVI = V<V<int>>;
using CVIR = const V<int>&;
static VVI findDifference(CVIR, CVIR) noexcept;
};
LC230503::VVI LC230503::findDifference(CVIR x, CVIR y) noexcept {
using USI = std::unordered_set<int>;
USI sx(x.begin(), x.end()), sy(y.begin(), y.end());
auto proc = [&](const USI& p, const USI& q) {
V<int> r;
std::copy_if(p.begin(), p.end(), std::back_inserter(r), [&](int n) {
return !q.count(n);
});
return r;
};
return {proc(sx, sy), proc(sy, sx)};
}

View File

@ -2,6 +2,7 @@
#define LEETCODE_CPP_DEFS_H #define LEETCODE_CPP_DEFS_H
#include <vector> #include <vector>
#include <string>
class LC230501CN { class LC230501CN {
public: public:
@ -29,4 +30,14 @@ public:
static bool isValid(const std::string&) noexcept; static bool isValid(const std::string&) noexcept;
}; };
class LC230503 {
public:
template<typename T>
using V = std::vector<T>;
using VVI = V<V<int>>;
using CVIR = const V<int>&;
static VVI findDifference(CVIR, CVIR) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H #endif //LEETCODE_CPP_DEFS_H