题目来源:
https://leetcode-cn.com/problems/sort-array-by-parity-ii/
题目描述:
代码如下:
class Solution {
public int[] sortArrayByParityII(int[] A) {
int j = 1;
for (int i = 0; i < A.length; i += 2) {
//A[i]为奇数
if ((A[i] & 1) != 0) {
//找出下标为奇数,但是A[j]是偶数的那个位置
while ((A[j] & 1) != 0) {
j += 2;
}
//交换A[i]和A[j]
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
return A;
}
}