-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReOrderOddEven.cpp
More file actions
62 lines (57 loc) · 1.29 KB
/
Copy pathReOrderOddEven.cpp
File metadata and controls
62 lines (57 loc) · 1.29 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
调整数组顺序使奇数位于偶数前面
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有
奇数位于数组的前半部分,所有偶数位于数组的后半部分。
*/
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> exchange(vector<int> &nums)
{
int left = 0, right = nums.size() - 1;
while (left < right)
{
while (left < right && (nums[left] & 1) == 1)
{
left++;
}
while (left < right && (nums[right] & 1) == 0)
{
right--;
}
if (left < right)
swap(nums[left], nums[right]);
}
return nums;
}
vector<int> exchange1(vector<int> &nums)
{
int left = 0, right = nums.size() - 1;
while (left < right)
{
if ((nums[left] & 1) != 0)
{
left++;
continue;
}
if ((nums[right] & 1) != 1)
{
right--;
continue;
}
swap(nums[left], nums[right]);
}
return nums;
}
int main()
{
vector<int> arr;
arr = {2, 3, 1, 4, 5};
exchange(arr);
for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
{
printf("%d ", *it);
}
return 0;
}