Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
음수가 없는 정수의 배열이 주어질 때, 모든 짝수 요소들이 앞에 오고 홀수들이 뒤따르는 배열을 반환하라.
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
class Solution {
public int[] sortArrayByParity(int[] A) {
int i = 0;
int j = A.length-1;
while(j-i >= 1){
int temp;
int begin = A[i]%2;
int end = A[j]%2;
if(begin == 0){
i++;
} else if (begin == 0 && end != 0){
i++;
} else if (end != 0){
j--;
} else if (begin != 0 && end == 0){
temp = A[j];
A[j] = A[i];
A[i] = temp;
i++;
j--;
}
}
return A;
}
}