剑指offer题解【调整数组顺序使奇数位于偶数前面】

来源:互联网 发布:房产中介网站源码 编辑:程序博客网 时间:2024/04/29 17:44
import java.util.ArrayList;public class Solution {    public static int[] reOrderArray(int[] array) {        ArrayList<Integer> oddList = new ArrayList<Integer>();        ArrayList<Integer> evenList = new ArrayList<Integer>();        for (int a : array) {            if (a % 2 == 0) {                evenList.add(a);            } else {                oddList.add(a);            }        }        oddList.addAll(evenList);        int size = oddList.size();        Integer[] inteArr = oddList.toArray(new Integer[size]);        for(int i = 0; i < size; i++) {            array[i] = inteArr[i].intValue();        }        return array;    }}

思路是将数组分开,再合并起来。Integer转int,可以使用.intValue()。

0 0