Love java8 lambda. 4lines solution


  • 5
    B

    4 lines only.

      public String largestNumber(int[] num) {
    
        String[] ns = Arrays.stream(num).mapToObj(x -> "" + x).toArray(String[]::new);
    
        Arrays.sort(ns, (String x, String y) -> (y + x).compareTo(x + y));
    
        if("0".equals(ns[0])) return "0";
    
        return Arrays.stream(ns).reduce((x, y) -> x + y).get();
      }

  • 1
    D

    very ugly code to be honest. Though it it is short, in my company we don't appriciate this kinda smart tricks.


  • 0
    B

    However I think this is easier to be understood.

    1. Prepare a string[]
    2. sort the string[]
      3 edge case
      4 join the sorted string[]

  • 0
    B

    That is really just different style of coding. I tried to solve this problem with traditional java style and java8 lamda/steam. The code will be very concise in this new style.

    Also, I am not sure why people tend to make their code to have fewer lines as possible. That makes the code ugly. Maybe it will be better to use the standard java lamda style:

    public String largestNumber(int[] num) {
        if(num == null || num.length == 0)
            return "";
        
        if(Arrays.stream(num).allMatch(n -> n == 0))
            return "0";
        
        StringBuffer sb = new StringBuffer();
        Arrays.stream(num)
               .mapToObj(n -> String.valueOf(n))
               .sorted((a, b) -> -((a + b).compareTo(b + a)))
               .forEachOrdered(n -> sb.append(n));
        
        return sb.toString();
    }

  • 0
    M

    Mine is twice 10 times longer, but twice faster :)

        if (num == null || num.length == 0) {
            return "";
        }
        
        String[] str = new String[num.length];
        for (int i = 0; i < str.length; i++) { 
            str[i] = String.valueOf(num[i]);
        }
        
        Arrays.sort(str, new Comparator<String>() {
        	@Override
            public int compare(String s, String t) {
                int i = 0;
                while (i < s.length() && i < t.length()) {
                    if (s.charAt(i) < t.charAt(i)) {
                        return -1;
                    } else if (s.charAt(i) > t.charAt(i)) {
                        return 1;
                    } else {
                        i++;
                    }
                }
        
                if (i == s.length() && i == t.length()) {
                    return 0;
                } else if (i == s.length()) {
                    // s is shorter
                    return compare(s, t.substring(i));
                } else {
                    // t is shorter
                    return compare(s.substring(i), t);
                }
            }
        });
        
        if (str[str.length - 1].equals("0")) {
        	return "0";
        }
        
        String result = "";
        for (int i = str.length - 1; i >= 0; i--) {
            result += str[i];
        }
        
        return result;
    

  • 0
    Z

    It's not some sort of "smart tricks". It's called functional programming style. Deal with it :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.