Odd and even number of sorted array

Given a non-negative integer array A, returns an array in the array, all even after element A along all odd elements.

You can return any array satisfies this condition as the answer.

Example:

Input: [3,1,2,4]
Output: [2,4,3,1]
Output [4,2,3,1], [2,4,1,3] and [4,2,1, 3] will be accepted.

class Solution {
    public int[] sortArrayByParity(int[] A) {
    int i=0;
    int j=A.length-1;
    while(i<j){
        if(A[i]%2==1&&A[j]%2==0){
            int tmp=A[i];
            A[i]=A[j];
            A[j]=tmp;
            i++;
            j--;
        }
        if(A[i]%2==0){
            i++;
        }if(A[j]%2==1){
            j--;
        }
    }
    return A;
    }
}
Published 71 original articles · won praise 3 · Views 1048

Guess you like

Origin blog.csdn.net/qq_44262984/article/details/104249298