[LeetCode] Sort Array By Parity

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
sort(begin(A), end(A), [](int& a, int& b) {
bool _a = a&1, _b = b&1;
if(_a == _b) return a<b;
return _b;
});
return A;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/02/PS/LeetCode/sort-array-by-parity/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.