Ad
Code
Diff
  • package space;
    import static java.util.stream.Collectors.joining;
    
    public class SpaceMaker{
      public static String spaceMakerV1(String str){
        return str.chars()
                  .mapToObj(c -> String.valueOf((char)c))
                  .collect(joining(" "));
      }
      public static String spaceMakerV2(String str){
        char[] chars = str.toCharArray();
        StringBuilder spaced = new StringBuilder();
        for(char c:chars){
          spaced.append(c).append(' ');
        }
        return spaced.toString().trim();
      }
    }
    • function spaceMaker(str){
    • return str.split("").join(" ");
    • package space;
    • import static java.util.stream.Collectors.joining;
    • public class SpaceMaker{
    • public static String spaceMakerV1(String str){
    • return str.chars()
    • .mapToObj(c -> String.valueOf((char)c))
    • .collect(joining(" "));
    • }
    • public static String spaceMakerV2(String str){
    • char[] chars = str.toCharArray();
    • StringBuilder spaced = new StringBuilder();
    • for(char c:chars){
    • spaced.append(c).append(' ');
    • }
    • return spaced.toString().trim();
    • }
    • }
Code
Diff
  • import java.util.stream.*;
    
    class Solution {
    
      public static String largestNumber(final Integer[] nums) {
        return Stream.of(nums)
          .sorted((a,b)->(""+b+a).compareTo(""+a+b))
          .collect(Collectors.reducing("",(x)->""+x,String::concat));
      }
    }
    • import java.util.stream.*;
    • class Solution {
    • public static String largestNumber(final Integer[] nums) {
    • return Stream.of(nums)
    • .sorted((a,b)->(""+b+a).compareTo(""+a+b))
    • .map(n->""+n)
    • .collect(Collectors.joining());
    • .collect(Collectors.reducing("",(x)->""+x,String::concat));
    • }
    • }
Code
Diff
  • import java.util.*;
    
    class Solution {
    
      public static String largestNumber(final Integer[] nums) {
      
        Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) );
        
        String result = "";
        for (final Integer num : nums) {
          result += num;
        }
        
        return result;
      }
    }
    • import java.util.*;
    • class Solution {
    • public static String largestNumber(final Integer[] nums) {
    • Arrays.sort(nums, new Comparator<Integer>() {
    • @Override
    • public int compare(final Integer a, final Integer b) {
    • return (""+b+a).compareTo(""+a+b);
    • }
    • });
    • Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) );
    • String result = "";
    • for (final Integer num : nums) {
    • result += num;
    • }
    • return result;
    • }
    • }