From 257eded1cc1575854e339dfadaa694bad1087f72 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sun, 8 May 2022 16:25:37 +0800 Subject: [PATCH] add: 220508-CN [cpp] --- cpp/2205/220508-CN.cpp | 24 ++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220508-CN.cpp diff --git a/cpp/2205/220508-CN.cpp b/cpp/2205/220508-CN.cpp new file mode 100644 index 0000000..e084ef0 --- /dev/null +++ b/cpp/2205/220508-CN.cpp @@ -0,0 +1,24 @@ +#include +#include + +/** + * 442. Find All Duplicates in an Array + * Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice. + * You must write an algorithm that runs in O(n) time and uses only constant extra space. + */ + +class Solution { +public: + static std::vector findDuplicates(const std::vector& nums) { + std::bitset<100001> s; + std::vector v; + v.reserve(nums.size()); + for (auto i : nums) { + if (s.test(i)) + v.push_back(i); + else + s.set(i); + } + return v; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 6eb00bb..6be1653 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 220508.cpp) +ADD_EXECUTABLE(2205 220508-CN.cpp)