讨论/《春招冲刺班 - 7 天刷题挑战》 - 数组组成最大数/
《春招冲刺班 - 7 天刷题挑战》 - 数组组成最大数

import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);

    String str = scanner.nextLine();
    scanner.close();
    String[] nums = str.substring(1,str.length() - 1).split(",");
    quicksort(nums,0,nums.length - 1);
    StringBuilder sb = new StringBuilder();
    for(String s : nums){
        sb.append(s);
    }
    System.out.print(sb.toString());
}


public static void quicksort(String[] nums,int left,int right){
    if(left > right){
        return;
    }

    int i = left, j = right;
    while(i < j){
        while((nums[left] + nums[j]).compareTo(nums[j] + nums[left]) >= 0 && i < j){
            j--;
        }
        
        while((nums[i] + nums[left]).compareTo(nums[left] + nums[i]) >= 0 && i < j){
            i++;
        }
        swap(nums,i,j);
    }
    swap(nums,left,i);
    quicksort(nums,left,i - 1);
    quicksort(nums,i + 1,right);
}

public static void swap(String[] nums,int i,int j){
    String temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
}

}

展开全部 18 讨论