diff --git a/cpp/2305/LC230503.cpp b/cpp/2305/LC230503.cpp new file mode 100644 index 0000000..8b321eb --- /dev/null +++ b/cpp/2305/LC230503.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +/** + * 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 + using V = std::vector; + using VVI = V>; + using CVIR = const V&; + + static VVI findDifference(CVIR, CVIR) noexcept; +}; + +LC230503::VVI LC230503::findDifference(CVIR x, CVIR y) noexcept { + using USI = std::unordered_set; + USI sx(x.begin(), x.end()), sy(y.begin(), y.end()); + auto proc = [&](const USI& p, const USI& q) { + V 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)}; +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index 0ddf4d3..1b24ff2 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -2,6 +2,7 @@ #define LEETCODE_CPP_DEFS_H #include +#include class LC230501CN { public: @@ -29,4 +30,14 @@ public: static bool isValid(const std::string&) noexcept; }; +class LC230503 { +public: + template + using V = std::vector; + using VVI = V>; + using CVIR = const V&; + + static VVI findDifference(CVIR, CVIR) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H