-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-Array-By-Parity-II.cpp
More file actions
40 lines (40 loc) · 998 Bytes
/
Sort-Array-By-Parity-II.cpp
File metadata and controls
40 lines (40 loc) · 998 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
40
class Solution
{
public:
vector<int> sortArrayByParityII(vector<int> &nums)
{
int ptrodd = 0, ptreven = 0, n = nums.size();
while (ptrodd < n && ptreven < n)
{
while (ptrodd < n)
{
if (ptrodd & 1)
{
if (nums[ptrodd] & 1)
ptrodd++;
else
break;
}
else
ptrodd++;
};
while (ptreven < n)
{
if ((ptreven & 1) == 0)
{
if ((nums[ptreven] & 1) == 0)
ptreven++;
else
break;
}
else
ptreven++;
};
if (ptrodd < n && ptreven < n)
{
swap(nums[ptrodd], nums[ptreven]);
};
};
return nums;
}
};