-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
33 lines (30 loc) · 772 Bytes
/
Copy pathmain.cpp
File metadata and controls
33 lines (30 loc) · 772 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
if (nums.size() < 2) return nums;
auto afterEven = nums.begin();
while (*afterEven % 2 == 0 && afterEven != nums.end()) afterEven += 1;
auto it = afterEven;
if (it == nums.end()) return nums;
while (it != nums.end()) {
if (*it % 2 == 0) {
swap(*it, *afterEven);
afterEven += 1;
}
it += 1;
}
return nums;
}
};
int main() {
Solution sol;
vector<int> testArr = {0, 2};
auto res = sol.sortArrayByParity(testArr);
for (auto i : res) {
cout << i << ' ';
}
return 0;
}