Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Stack;
- public class MaxMergedNumberForIntArray {
- private static Comparator<Integer> MAX_MERGED_COMPARATOR = (x, y) -> {
- final Stack<Integer> xDigits = getStuckWithDigits(x);
- final Stack<Integer> yDigits = getStuckWithDigits(y);
- while (!xDigits.empty() && !yDigits.empty()) {
- final Integer xValue = xDigits.pop();
- final Integer yValue = yDigits.pop();
- if (xValue > yValue) {
- return 1;
- } else if (xValue < yValue) {
- return -1;
- }
- }
- if (xDigits.empty() && yDigits.empty()) {
- return 0;
- } else if (xDigits.empty() && !yDigits.empty()) {
- return 1;
- } else {
- return -1;
- }
- };
- private static Stack<Integer> getStuckWithDigits(Integer o1) {
- final Stack<Integer> o1Integers = new Stack<>();
- while (o1 > 0) {
- o1Integers.push(o1 % 10);
- o1 /= 10;
- }
- return o1Integers;
- }
- public static String maxMergedNumber(Integer... numbers) {
- Arrays.sort(numbers, MAX_MERGED_COMPARATOR.reversed());
- StringBuilder sb = new StringBuilder();
- for (Integer number : numbers) {
- sb.append(number);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement