Leetcode 941 Solution
This article provides solution to leetcode question 941 (sort-array-by-parity)
Access this page by simply typing in "lcs 941" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/sort-array-by-parity
Solution
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
int l = 0;
int r = A.size() - 1;
while (l < r)
{
while (A[l] % 2 == 0 && l < r)
l++;
while (A[r] % 2 == 1 && l < r)
r--;
if (l < r)
swap(A[l++], A[r--]);
}
return A;
}
};