Skip to content

Commit 7fa5579

Browse files
authored
Create sort-array-by-parity.cpp
1 parent 7929c3c commit 7fa5579

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

C++/sort-array-by-parity.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> sortArrayByParity(vector<int>& A) {
7+
for (int i = 0, j = 0; j < A.size(); ++j) {
8+
if (A[j] % 2 == 0) {
9+
swap(A[i++], A[j]);
10+
}
11+
}
12+
return A;
13+
}
14+
};

0 commit comments

Comments
 (0)