forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2231.java
37 lines (35 loc) · 1.19 KB
/
_2231.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
package com.fishercoder.solutions;
import java.util.*;
public class _2231 {
public static class Solution1 {
public int largestInteger(int num) {
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
PriorityQueue<Integer> oddTimes = new PriorityQueue<>();
PriorityQueue<Integer> evenTimes = new PriorityQueue<>();
int times = 1;
while (num != 0) {
int digit = num % 10;
num /= 10;
if (digit % 2 == 0) {
evens.add(digit);
evenTimes.offer(times);
} else {
odds.add(digit);
oddTimes.offer(times);
}
times *= 10;
}
Collections.sort(evens);
Collections.sort(odds);
int composite = 0;
for (int i = 0; i < odds.size(); i++) {
composite += odds.get(i) * oddTimes.poll();
}
for (int i = 0; i < evens.size(); i++) {
composite += evens.get(i) * evenTimes.poll();
}
return composite;
}
}
}