-
Notifications
You must be signed in to change notification settings - Fork 6
/
SortArrayByParityII.java
41 lines (39 loc) · 1.16 KB
/
SortArrayByParityII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package io.ziheng.array.leetcode;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* LeetCode 922. Sort Array By Parity II
* https://leetcode.com/problems/sort-array-by-parity-ii/
*/
public class SortArrayByParityII {
public int[] sortArrayByParityII(int[] A) {
if (A == null || A.length == 0) {
return new int[0];
}
List<Integer> oddList = new ArrayList<>();
List<Integer> evenList = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
if (isEven(A[i])) {
evenList.add(A[i]);
} else {
oddList.add(A[i]);
}
}
Collections.sort(oddList);
Collections.sort(evenList);
int[] resultArray = new int[A.length];
for (int i = 0; i < resultArray.length; i++) {
if (isEven(i)) {
resultArray[i] = evenList.remove(evenList.size() - 1);
} else {
resultArray[i] = oddList.remove(oddList.size() - 1);
}
}
return resultArray;
}
private boolean isEven(int num) {
return num % 2 == 0;
}
}
/* EOF */