> For the complete documentation index, see [llms.txt](https://zhongquan789.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zhongquan789.gitbook.io/leetcode/lcof/mian-shi-ti-21.md).

# 面试题21

输入一个整数数组，实现一个函数来调整该数组中数字的顺序，使得所有奇数位于数组的前半部分，所有偶数位于数组的后半部分。

```
示例：

输入：nums = [1,2,3,4]
输出：[1,3,2,4] 
注：[3,1,2,4] 也是正确的答案之一。
```

## 提示：

* 1 <= nums.length <= 50000
* 1 <= nums\[i] <= 10000

## Solutions

1. **two pointers**

```cpp
class Solution {
public:
    vector<int> exchange(vector<int>& nums) {
        int i = 0, j = nums.size() - 1;

        while (i < j)
            if (nums[i] % 2 == 1)
                i++;
            else if (nums[j] % 2 == 0)
                j--;
            else
                swap(nums[i], nums[j]);

        return nums;
    }
};
```

Another approach is stacking odd numbers at the beginning of array, however, there are too much assignment operations in this approach.
