From 61f66c394f2cc85cd026a94ff0e39bac05c9d939 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 15 Feb 2022 12:08:02 +0800 Subject: [PATCH] add: 220215 [previous is not CN] improved --- 2202/220215.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/2202/220215.cpp b/2202/220215.cpp index a79fb0d..a02fb12 100644 --- a/2202/220215.cpp +++ b/2202/220215.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -9,6 +10,32 @@ */ class Solution { +public: + static int singleNumber(const std::vector& nums) { + int x = 0; + for (int i : nums) + x = x xor i; + return x; + } +}; + +class SolutionOld { +private: + inline static const int maxN = 30000 + 3; +public: + static int singleNumber(const std::vector& nums) { + int cnt[maxN << 1]{}; + for (int i : nums) + ++cnt[i + maxN]; + for (int i = 0; i < (maxN << 1); ++i) + if (cnt[i] == 1) + return i - maxN; + assert(false); + return -1; + } +}; + +class SolutionOldOld { public: static int singleNumber(const std::vector& nums) { std::unordered_set s; @@ -22,6 +49,6 @@ public: }; int main() { - std::cout << Solution::singleNumber({4,1,2,1,2}); + std::cout << Solution::singleNumber({1}); return 0; }