顿搜
飞过闲红千叶,夕岸在哪
类目归类
Partition an integers array into odd number first and even number second.
void partitionArray(vector<int>& nums)
{
if (nums.empty()) {
return;
}
int start = 0, end = nums.size() - 1;
while (start < end) {
while (start < end && (nums[start] & 1) == 1)
start++;
while (start < end && (nums[end] & 1) == 0)
end--;
if (start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
}
}
}#include <iostream>
#include <vector>
using namespace std;
class LintCode0373 {
public:
void partitionArray(vector<int>& nums)
{
if (nums.empty()) {
return;
}
int start = 0, end = nums.size() - 1;
while (start < end) {
while (start < end && (nums[start] & 1) == 1)
start++;
while (start < end && (nums[end] & 1) == 0)
end--;
if (start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
}
}
}
};
int main()
{
LintCode0373 lintcode;
int numbers[] = { 1, 2, 3, 4 };
vector<int> nums(numbers, numbers + 4);
lintcode.partitionArray(nums);
for (int i = 0; i < nums.size(); i++) {
cout << nums[i];
}
}