> For the complete documentation index, see [llms.txt](https://dfine.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://dfine.gitbook.io/leetcode/922.sort_array_by_parity_ii.md).

# 922.Sort Array by Parity II

**922.Sort Array by Parity II**

难度:Easy

> 给定一个非负整数数组 A， A 中一半整数是奇数，一半整数是偶数。 对数组进行排序，以便当 A\[i] 为奇数时，i 也是奇数；当 A\[i] 为偶数时， i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。

```
示例：

输入：[4,2,5,7]
输出：[4,5,2,7]
解释：[4,7,2,5]，[2,5,4,7]，[2,7,4,5] 也会被接受。
 

提示：

2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
```

代码如下：

```
class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& A) {
        vector<int> res(A.size(),0);
        int s1=0,s2=1;
        for(int i=0;i<A.size();i++)
        {
            if(A[i]&1)
            {
                res[s2]=A[i];
                s2+=2;
            }
            else
            {
                res[s1]=A[i];
                s1+=2;
            }
        }
        return res;
    }
};
```
