add: 220215-CN

This commit is contained in:
Lam Haoyin 2022-02-15 11:57:08 +08:00
parent 512775b7be
commit 152272dcb1
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 28 additions and 1 deletions

27
2202/220215.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <vector>
#include <iostream>
#include <unordered_set>
/**
* 136. Single Number
* Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
* You must implement a solution with a linear runtime complexity and use only constant extra space.
*/
class Solution {
public:
static int singleNumber(const std::vector<int>& nums) {
std::unordered_set<int> s;
for (int i : nums)
if (s.count(i))
s.erase(i);
else
s.insert(i);
return *s.begin();
}
};
int main() {
std::cout << Solution::singleNumber({4,1,2,1,2});
return 0;
}

View File

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