add: 220521-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-21 19:25:40 +08:00
parent 0969951471
commit 3a3cc21bb2
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 25 additions and 1 deletions

24
cpp/2205/220521-CN.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <vector>
#include <unordered_set>
/**
* 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<int>& nums) {
std::unordered_set<int> s;
for (int i : nums) {
if (s.count(i))
return i;
s.insert(i);
}
return -1;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220521.cpp)
ADD_EXECUTABLE(2205 220521-CN.cpp)