add: 220428-CN [cpp]

This commit is contained in:
eat-swap 2022-04-28 00:15:36 +08:00
parent 8c0ffa7567
commit ca16bc6381
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 19 additions and 1 deletions

18
cpp/2204/220428-CN.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <vector>
/**
* 905. Sort Array By Parity
* Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.
* Return any array that satisfies this condition.
*/
class Solution {
public:
std::vector<int> sortArrayByParity(std::vector<int>& nums) {
std::vector<int> e, o;
for (int i : nums)
((i & 1) ? &o : &e)->push_back(i);
e.insert(e.end(), o.begin(), o.end());
return e;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220427-CN.cpp) ADD_EXECUTABLE(2204 220428-CN.cpp)