From 152272dcb136e4757caf918f89c4888f0fab3ff6 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 15 Feb 2022 11:57:08 +0800 Subject: [PATCH] add: 220215-CN --- 2202/220215.cpp | 27 +++++++++++++++++++++++++++ 2202/CMakeLists.txt | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 2202/220215.cpp diff --git a/2202/220215.cpp b/2202/220215.cpp new file mode 100644 index 0000000..a79fb0d --- /dev/null +++ b/2202/220215.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +/** + * 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& nums) { + std::unordered_set 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; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index 3ea8ac5..7d0ba3b 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220215-CN.cpp) +ADD_EXECUTABLE(2202 220215.cpp)