forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2164.java
32 lines (30 loc) · 984 Bytes
/
_2164.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _2164 {
public static class Solution1 {
public int[] sortEvenOdd(int[] nums) {
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
evens.add(nums[i]);
} else {
odds.add(nums[i]);
}
}
Collections.sort(odds, Collections.reverseOrder());
Collections.sort(evens);
int[] ans = new int[nums.length];
for (int i = 0, j = 0, k = 0; i < nums.length; i++) {
if (i % 2 == 0) {
ans[i] = evens.get(j++);
} else {
ans[i] = odds.get(k++);
}
}
return ans;
}
}
}