分割一个整数数组,使得奇数在前偶数在后。
样例:
给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]。
挑战:
在原数组中完成,不使用额外空间。
#ifndef C373_H
#define C373_H
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
/*
* @param nums: an array of integers
* @return: nothing
*/
void partitionArray(vector<int> &nums) {
// write your code here
int i = 0;
int j = nums.size()-1;
while (i<j)
{
if (nums[i] %2 == 0 && nums[j] %2!=0)
{
swap(nums[i], nums[j]);
i++;
j--;
}
if (nums[i] %2!=0)
i++;
if (nums[j] %2 == 0)
j--;
}
}
};
#endif