905. 按奇偶排序数组

给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。

你可以返回满足此条件的任何数组作为答案。

示例:

输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。

提示:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

思路:  同时从头尾开始遍历,   目前 beat 100%

class Solution {
    public int[] sortArrayByParity(int[] A) {
        int i=0,j=A.length-1;
        for(;i<j;){
            if((A[i]&1)==1 && (A[j]&1)==0 ){
                swap(A,i,j);
                ++i;--j;
            } else {
                if((A[i]&1)==0) ++i;
                if((A[j]&1)==1) --j;
            }
        }
        return A;
    }
    int temp=0;
    public void swap(int[] A,int i,int j){
        temp = A[i];
        A[i]=A[j];
        A[j] = temp;
    }
}
原文地址:https://www.cnblogs.com/chen-jack/p/9936596.html