Leetcode 941 Solution

This article provides solution to leetcode question 941 (sort-array-by-parity)

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; } };