Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created August 22, 2020 03:33
Show Gist options
  • Save lishunan246/9101bdc9c29649faf19b0515ecebcd3d to your computer and use it in GitHub Desktop.
Save lishunan246/9101bdc9c29649faf19b0515ecebcd3d to your computer and use it in GitHub Desktop.
Sort Array By Parity
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
int back = A.size() - 1;
for (int i = 0; i < back; ++i) {
if (A[i] % 2 == 1) {
std::swap(A[back], A[i]);
back--;
i--;
}
}
return A;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment