From eedc44ba5c4c0b577a2476b03523ec10dee2d923 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 26 Mar 2023 18:40:00 +0800 Subject: [PATCH] add: 230326-CN --- cpp/2303/230326-CN.cpp | 24 ++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230326-CN.cpp diff --git a/cpp/2303/230326-CN.cpp b/cpp/2303/230326-CN.cpp new file mode 100644 index 0000000..3bee197 --- /dev/null +++ b/cpp/2303/230326-CN.cpp @@ -0,0 +1,24 @@ +#include +#include + +/** + * 2395. Find Subarrays With Equal Sum + * + * Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices. + * Return true if these subarrays exist, and false otherwise. + * A subarray is a contiguous non-empty sequence of elements within an array. + */ + +class Solution { +public: + static bool findSubarrays(const std::vector& nums) { + const int n = nums.size(); + std::unordered_set s; + for (int i = 1; i < n; ++i) + if (s.count(nums[i] + nums[i - 1])) + return true; + else + s.insert(nums[i] + nums[i - 1]); + return false; + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 093ba78..ca81fac 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230325.cpp) +ADD_EXECUTABLE(2303 230326-CN.cpp)