题目
代码部分一(18ms)
class Solution {
public int[] sortArrayByParity(int[] A) {
List<Integer> odd = new ArrayList(); // 储存奇数位
List<Integer> even = new ArrayList(); // 储存偶数位
for(int i = 0; i < A.length; i++){
if(A[i] %2 == 0)
even.add(A[i]);
else
odd.add(A[i]);
}
int oddLen = odd.size();
int evenLen = even.size();
int[] res = new int[oddLen + evenLen];
for(int j = 0; j < evenLen; j++)
res[j] = even.get(j);
for(int x = 0; x < oddLen; x++)
res[x+evenLen] = odd.get(x);
return res;
}
}