add: 220206-CN

This commit is contained in:
Lam Haoyin 2022-02-06 09:16:18 +08:00
parent 078e3a226c
commit 70c097ca7e
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 33 additions and 1 deletions

32
2202/220206-CN.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <vector>
#include <unordered_set>
/**
* 1748. Sum of Unique Elements
* You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.
* Return the sum of all the unique elements of nums.
*/
class Solution {
public:
static int sumOfUnique(const std::vector<int>& nums) {
int ans = 0;
std::unordered_multiset<int> s;
for (int x : nums) {
switch (s.count(x)) {
case 0:
ans += x;
break;
case 1:
ans -= x;
}
s.insert(x);
}
return ans;
}
};
int main() {
// No testing code.
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220205.cpp) ADD_EXECUTABLE(2202 220206-CN.cpp)