-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsort-array-by-parity.cpp
More file actions
39 lines (32 loc) · 865 Bytes
/
Copy pathsort-array-by-parity.cpp
File metadata and controls
39 lines (32 loc) · 865 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
class Solution {
public:
// new array
//
// vector<int> sortArrayByParity(vector<int>& nums) {
// int n = size(nums);
// int start = 0 , end = n - 1;\
// vector<int> ans(n);
// for(auto x : nums) {
// if(x & 1 == 0) ans[start++] = x;
// else ans[end--] = x;
// }
// return ans ;
// }
void swap(int &a, int& b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
// in place
vector<int> sortArrayByParity(vector<int>& nums) {
int length = size(nums);
int start = 0;
for( int i = 0; i < length; i++ ) {
if ( ( nums[i] & 1 ) == 0) {
if ( i != start) swap(nums[i], nums[start]);
start++;
}
}
return nums;
}
};