One pass in-place solution: all swaps.
class Solution { public: /** * @param nums: a vector of integers * @return: nothing */ void partitionArray(vector<int> &nums) { size_t len = nums.size(); int i = 0, io = 0, ie = len - 1; while (io < ie) { int v = nums[i]; if (v & 0x1) // odd { swap(nums[i], nums[io++]); } else // even { swap(nums[i], nums[ie--]); } i = io; } } };
时间: 2024-11-15 10:45:13