1. Description
Partition an integers array into odd number first and even number second.
2. Submission Details
3. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C++" active="active"]
void partitionArray(vector& 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; } } }
[/restab]
[/restabs]
4.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C++" active="active" ]
#include#include using namespace std; class LintCode0373 { public: void partitionArray(vector & 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 nums(numbers, numbers + 4); lintcode.partitionArray(nums); for (int i = 0; i < nums.size(); i++) { cout << nums[i]; } }
[/restab]
[/restabs]
Comments | NOTHING