add: 220508-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-08 16:25:37 +08:00
parent f0d37c99e9
commit 257eded1cc
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 25 additions and 1 deletions

24
cpp/2205/220508-CN.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <vector>
#include <bitset>
/**
* 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<int> findDuplicates(const std::vector<int>& nums) {
std::bitset<100001> s;
std::vector<int> v;
v.reserve(nums.size());
for (auto i : nums) {
if (s.test(i))
v.push_back(i);
else
s.set(i);
}
return v;
}
};

View File

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