public class Solution {
public int[] SortArrayByParity(int[] A) {
int[] arr = new int[A.Length];
int top = 0;
int bottom = A.Length - 1;
for (int i = 0;
i < A.Length;
i++)
{
if (A[i] % 2 == 0)
{
arr[top] = A[i];
top++;
}
else
{
arr[bottom] = A[i];
bottom--;
}
} 【905.|905. 按奇偶排序数组】return arr;
}
}
还有比较取巧的办法:
public class Solution {
public int[] SortArrayByParity(int[] A) {
int index = 0;
for (int i = 0;
i < A.Length;
i++) {
if ((A[i] & 1) == 0) {
int tmp = A[i];
A[i] = A[index];
A[index++] = tmp;
}
}
return A;
}
}