Skip to content

Commit

Permalink
Create sort-array-by-parity.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 16, 2018
1 parent f21d2d5 commit fe9e146
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions C++/sort-array-by-parity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Time: O(n)
// Space: O(1)

class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
for (int i = 0, j = 0; j < A.size(); ++j) {
if (A[j] % 2 == 0) {
swap(A[i++], A[j]);
}
}
return A;
}
};

0 comments on commit fe9e146

Please sign in to comment.