Created
August 22, 2020 03:33
-
-
Save lishunan246/9101bdc9c29649faf19b0515ecebcd3d to your computer and use it in GitHub Desktop.
Sort Array By Parity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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