blob: c612f9118294e04b00093e87ea97c3b4c04e0420 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include "header.hpp"
class Solution {
public:
static std::vector<int> sortArrayByParity(std::vector<int> &nums) {
std::vector<int> result;
int front = 0;
int end = nums.size() - 1;
result.resize(nums.size());
for (auto &iter : nums) {
if (iter % 2 == 0) {
result[front] = iter;
front++;
} else {
result[end] = iter;
end--;
}
}
for (int i = 0; i < result.size(); ++i) {
std::cout << result[i] << "\n";
}
return result;
}
};
int main(int argc, char **argv) {
std::vector<int> nums = {3, 1, 2, 4};
std::vector<int> nums2 = {0};
auto result = Solution::sortArrayByParity(nums);
auto result2 = Solution::sortArrayByParity(nums2);
for (auto &iter : result) {
std::cout << iter << "\n";
}
return 0;
}
|