forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
41 lines (33 loc) · 919 Bytes
/
main3.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
37
38
39
40
41
/// Source : https://leetcode.com/problems/sort-array-by-parity-ii/description/
/// Author : liuyubobobo
/// Time : 2018-10-14
#include <iostream>
#include <vector>
using namespace std;
/// Make the change in place
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
for(int i = 0; i < A.size(); i ++)
if(i % 2 == 0 && A[i] % 2){
int j = i + 1;
for(; j < A.size(); j += 2)
if(A[j] % 2 == 0)
break;
swap(A[i], A[j]);
}
else if(i % 2 && A[i] % 2 == 0){
int j = i + 1;
for(; j < A.size(); j += 2)
if(A[j] % 2)
break;
swap(A[i], A[j]);
}
return A;
}
};
int main() {
return 0;
}