-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path922.cpp
36 lines (36 loc) · 1.16 KB
/
922.cpp
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
__________________________________________________________________________________________________
sample 76 ms submission
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
ios::sync_with_stdio(false);
int i, j, len = A.size();
for (i = 0, j = 1; i < len && j < len; i += 2, j += 2) {
while (i < len && A[i] % 2 == 0) i += 2;
while (j < len && A[j] % 2 != 0) j += 2;
if (i < len && j < len) swap(A[i], A[j]);
}
return A;
}
};
__________________________________________________________________________________________________
sample 11960 kb submission
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> sorted(A.size(), 0);
int oddCounter = 1;
int evenCounter = 0;
for (int i = 0; i < A.size(); ++i) {
if (A[i] % 2 == 0) {
sorted[evenCounter] = A[i];
evenCounter += 2;
} else {
sorted[oddCounter] = A[i];
oddCounter += 2;
}
}
return sorted;
}
};
__________________________________________________________________________________________________