把数组由大到小排序,并连接成为字符串

class Solution {
    public String largestNumber(int[] nums) {
    String[] numStrs = new String[nums.length];
        for(int i = 0; i < nums.length; ++ i) {
            numStrs[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(numStrs, 
            (o1,o2)->(o1+o2).compareTo(o2+o1));
        StringBuilder sb = new StringBuilder();
        for(int i = nums.length-1; i >= 0; -- i) {
            sb.append(numStrs[i]);
        }
        if(sb.charAt(0) == '0') return "0";
        return sb.toString();
    }
}
import java.util.Arrays;

public class TestArray {

    public static String largestNumber(int[] nums) {
        String[] numStrs = new String[nums.length];
        for(int i = 0; i < nums.length; ++ i) {
            numStrs[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(numStrs,
                (o1,o2)->(o1+o2).compareTo(o2+o1));
        StringBuilder sb = new StringBuilder();
        for(int i = nums.length-1; i >= 0; -- i) {
            sb.append(numStrs[i]);
        }
        if(sb.charAt(0) == '0') return "0";
        return sb.toString();
    }


    public static void main(String args[]){
        int[] Nums = {1,2,3,4,5,6};
        String aaa = largestNumber(Nums);
        System.out.println(aaa);

    }
}

 

posted @ 2020-12-23 11:11  巴黎爱工作  阅读(192)  评论(0编辑  收藏  举报