-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortArrayByParity*
More file actions
39 lines (33 loc) · 788 Bytes
/
Copy pathSortArrayByParity*
File metadata and controls
39 lines (33 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*********************************************/
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
/********************************************/
class Solution {
public:
vector<int>sortArrayByParity(vector<int>&A) {
int size = A.size();
if (size <= 1) {
return A;
}
int m = 0;
int temp;
for (int i = 0; i < size; i++) {
if ((A[m] & 1) == 0) {
m++;
}
else {
if ((A[i] & 1) == 0) {
temp = A[i];
A[i] = A[m];
A[m] = temp;
m++;
}
}
}
return A;
}
};