From 3a3cc21bb2e5cc0b6b0bc9bdd1397e16309df5fb Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sat, 21 May 2022 19:25:40 +0800 Subject: [PATCH] add: 220521-CN [cpp] --- cpp/2205/220521-CN.cpp | 24 ++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220521-CN.cpp diff --git a/cpp/2205/220521-CN.cpp b/cpp/2205/220521-CN.cpp new file mode 100644 index 0000000..7fd685c --- /dev/null +++ b/cpp/2205/220521-CN.cpp @@ -0,0 +1,24 @@ +#include +#include + +/** + * 961. N-Repeated Element in Size 2N Array + * You are given an integer array nums with the following properties: + * - nums.length == 2 * n. + * - nums contains n + 1 unique elements. + * - Exactly one element of nums is repeated n times. + * Return the element that is repeated n times. + */ + +class Solution { +public: + static int repeatedNTimes(const std::vector& nums) { + std::unordered_set s; + for (int i : nums) { + if (s.count(i)) + return i; + s.insert(i); + } + return -1; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 119a00a..15e1b64 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220521.cpp) +ADD_EXECUTABLE(2205 220521-CN.cpp)