add: 220508-CN [cpp]
This commit is contained in:
parent
f0d37c99e9
commit
257eded1cc
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2205 220508.cpp)
|
||||
ADD_EXECUTABLE(2205 220508-CN.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue