This is the java solution for the Leetcode problem – Sort Array By Parity II – Leetcode Challenge – Java Solution.
Source – qiyuangong’s repository.
class Solution {
/*public int[] sortArrayByParityII(int[] A) {
int N = A.length;
int[] ans = new int[N];
int t = 0;
for (int x: A) if (x % 2 == 0) {
ans[t] = x;
t += 2;
}
t = 1;
for (int x: A) if (x % 2 == 1) {
ans[t] = x;
t += 2;
}
return ans;
}*/
public int[] sortArrayByParityII(int[] A) {
int j = 1;
for (int i = 0; i < A.length; i += 2)
if (A[i] % 2 == 1) {
while (A[j] % 2 == 1)
j += 2;
// Swap A[i] and A[j]
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
return A;
}
}