Question
Solution
题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序
思路:建两个list,一个放偶数,一个放奇数,最后将两个list合并,转化为数组返回
Java实现:
public int[] sortArrayByParity(int[] A) {
List<Integer> evenList = new ArrayList<>();
List<Integer> oddList = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
if (A[i] % 2 == 0) evenList.add(A[i]);
else oddList.add(A[i]);
}
evenList.addAll(oddList);
int[] retArr = new int[A.length];
for (int i = 0; i < evenList.size(); i++) {
retArr[i] = evenList.get(i);
}
return retArr;
}