From e112f8c395045b1fc374203120a2544918cba81a Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sat, 28 May 2022 12:50:40 +0800 Subject: [PATCH] add: 220528 [cpp] --- cpp/2205/220528.cpp | 16 ++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220528.cpp diff --git a/cpp/2205/220528.cpp b/cpp/2205/220528.cpp new file mode 100644 index 0000000..a64f2b1 --- /dev/null +++ b/cpp/2205/220528.cpp @@ -0,0 +1,16 @@ +#include + +/** + * 268. Missing Number + * Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. + */ + +class Solution { +public: + int missingNumber(const std::vector& nums) { + int ans = 0; + for (int i = 1; i <= nums.size(); ++i) + ans ^= i ^ nums[i - 1]; + return ans; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 6fafc44..b8cf7ea 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220527.cpp) +ADD_EXECUTABLE(2205 220528.cpp)