905.按奇偶数组排序

905.按奇偶数组排序**解题思路:**定义两个指针和一个新的数组,头尾同时遍历,如果是偶数,则从新数组头开始插入,如果是奇数,则从尾开始插入,直到数组遍历完成。

class Solution {
    public int[] sortArrayByParity(int[] A) {
        if(A==null||A.length==1){
            return A;
        }
        int[] newA=new int[A.length];
        int left=0;
        int right=A.length-1;
        for(int i=0,j=A.length-1;i<A.length/2;){
            if(A[i]%2==0){
                newA[left]=A[i];
                left++;
                i++;
            }else{
                newA[right]=A[i];
                right--;
                i++;
            }
            if(A[j]%2==0){
                newA[left]=A[j];
                left++;
                j--;
            }else{
                newA[right]=A[j];
                right--;
                j--;
            }
        }
        if(A.length%2==1){
           int temp=A[A.length/2];
            if(temp%2==0){
                newA[left]=temp;
            }else{
                newA[right]=temp;
            }
        }
        A=newA;
        return A;
    }
}