# 21.调整数组顺序使奇数位于偶数前面

## 题目描述

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

示例：

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

提示：

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

来源：力扣（LeetCode） 链接：<https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof> 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

## 解法一

最容易想到的

```swift
func exchange(_ nums: [Int]) -> [Int] {
    var temp: [Int] = []
    nums.forEach {
        if $0 & 1 == 1 {
            temp.insert($0, at: 0)
        }
        else {
            temp.append($0)
        }
    }
    return temp
}
```

## 解法二：差速双指针

配上这个动图食用更佳[来源](https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/solution/ti-jie-shou-wei-shuang-zhi-zhen-kuai-man-shuang-zh/)

![差速双指针](/files/-Me4Ovduz7n1fKufEZ-C)

```swift
func exchange1(_ nums: [Int]) -> [Int] {
    var fastP: Int = 0
    var slowP: Int = 0
    var temp = nums

    while fastP < nums.count {
        if temp[fastP] & 1 == 1 {
            temp.swapAt(fastP, slowP)
            slowP += 1
        }
        fastP += 1
    }

    return temp
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/shu-ju-jie-gou-yu-suan-fa/jian-zhi-offerswift/21.-tiao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
