Ad

Solucion con lambdas

Code
Diff
  • import java.util.Arrays;
    import java.util.Comparator;
    import java.util.stream.Collectors;
    import java.math.BigInteger;
    
    public class MaxNumber {
        public static BigInteger print(long number) {
          String numeroMaximo = Long.toString(number)                      
                        .chars()                              
                        .mapToObj(Character::toString)
                        .sorted((a, b) -> b.compareTo(a))
                        .collect(Collectors.joining());
          return new BigInteger(numeroMaximo);
        }
    }
    • import java.util.Arrays;
    • import java.util.Comparator;
    • import java.util.stream.Collectors;
    • import java.math.BigInteger;
    • public class MaxNumber {
    • public static BigInteger print(long number) {
    • int[] digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    • for (char digit : Long.toString(number).toCharArray()) {
    • digits[digit - '0']++;
    • }
    • BigInteger sum = BigInteger.ZERO;
    • int i = 9;
    • while(i >= 0) {
    • if (digits[i] == 0) {
    • i--;
    • } else {
    • digits[i]--;
    • sum = sum.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    • }
    • }
    • return sum;
    • String numeroMaximo = Long.toString(number)
    • .chars()
    • .mapToObj(Character::toString)
    • .sorted((a, b) -> b.compareTo(a))
    • .collect(Collectors.joining());
    • return new BigInteger(numeroMaximo);
    • }
    • }