-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0032.h
More file actions
37 lines (32 loc) · 874 Bytes
/
Problem0032.h
File metadata and controls
37 lines (32 loc) · 874 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
//
// Created by Fengwei Zhang on 2021/5/28.
//
#ifndef ACWINGSOLUTION_PROBLEM0032_H
#define ACWINGSOLUTION_PROBLEM0032_H
#include <vector>
#include <algorithm>
using namespace std;
class Problem0032 {
public:
void reOrderArray(vector<int> &array) {
if (array.size() < 2) {
return;
}
unsigned long left = 0;
unsigned long right = array.size() - 1; // 若array为空,这里会发生溢出。
while (left < right) {
while (left < right && array[left] % 2 == 1) {
++left;
}
while (left < right && array[right] % 2 == 0) {
--right;
}
if (left < right) {
swap(array[left], array[right]);
++left;
--right;
}
}
}
};
#endif //ACWINGSOLUTION_PROBLEM0032_H