forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
40 lines (31 loc) · 873 Bytes
/
main.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
/// Source : https://leetcode.com/problems/sort-array-by-parity-ii/description/
/// Author : liuyubobobo
/// Time : 2018-10-13
#include <iostream>
#include <vector>
using namespace std;
/// Seperate odd and even elements into different vectors
/// Time Complexity: O(n)
/// Space Complexity: O(2*n)
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> even, odd;
for(int i = 0; i < A.size(); i ++)
if(A[i] % 2)
odd.push_back(A[i]);
else
even.push_back(A[i]);
vector<int> ret;
int p_odd = 0, p_even = 0;
for(int i = 0; i < A.size(); i ++)
if(i % 2)
ret.push_back(odd[p_odd ++]);
else
ret.push_back(even[p_even ++]);
return ret;
}
};
int main() {
return 0;
}