From fe9e1461010d9ca8325df712c1ab8e7a88a6d3ea Mon Sep 17 00:00:00 2001 From: kamyu Date: Sun, 16 Sep 2018 19:02:23 +0800 Subject: [PATCH] Create sort-array-by-parity.cpp --- C++/sort-array-by-parity.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 C++/sort-array-by-parity.cpp diff --git a/C++/sort-array-by-parity.cpp b/C++/sort-array-by-parity.cpp new file mode 100644 index 000000000..cfa3e2641 --- /dev/null +++ b/C++/sort-array-by-parity.cpp @@ -0,0 +1,14 @@ +// Time: O(n) +// Space: O(1) + +class Solution { +public: + vector sortArrayByParity(vector& A) { + for (int i = 0, j = 0; j < A.size(); ++j) { + if (A[j] % 2 == 0) { + swap(A[i++], A[j]); + } + } + return A; + } +};