Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

 

 

给一组数,求这一组数的最大组合。

 

刚开始想用直接排序的方法:1、最高位较大的放在前面

2、但是就出现了54与5这种情况,那么进一步判断。

3、然后又出现了121与12的情况,就越写越复杂。也没有写对。

public class Solution {
    public String largestNumber(int[] nums) {
        System.out.println(compare(121,12));
        StringBuffer str = new StringBuffer();
        sort(nums, 0, nums.length - 1);
        for (int i = nums.length - 1;i >= 0; i--){
            System.out.print(nums[i]+" ");
            str.append(nums[i]);
        }
        return str.toString();
    }
    private void sort(int[] nums, int left, int right){
        if (left >= right){
            return ;
        }
        int start = left;
        int end = right;
        int flag = nums[left];
        while (left < right){
            while (right > left && compare(flag, nums[right]) == -1){
                right--;
            }
            if (left == right){
                break;
            } else {
                nums[left] = nums[right];
                nums[right] = flag;
                left++;
            }
            while (right > left && compare(nums[left],flag) == -1){
                left++;
            }
            if (left == right){
                break;
            } else {
                nums[right] = nums[left];
                nums[left] = flag;
                right--;
            }
            
        }
        for( int i = 0;i < nums.length; i++)
            System.out.print(nums[i]+" ");
        System.out.println();
        sort(nums, start, left - 1);
        sort(nums, right + 1, end);
    }
    private int compare(int num1, int num2){
        double num1_copy = num1;
        double num2_copy = num2;
        while (num1_copy >= 10){
            num1_copy = num1_copy / 10;
        }
        while (num2_copy >= 10){
            num2_copy = num2_copy / 10;
        }
        if ((int) num1_copy % 10 > (int) num2_copy % 10){
            return 1;
        } else if ((int) num1_copy % 10 < (int) num2_copy % 10){
            return -1;
        } else {
            int flag = (int) num1_copy % 10;
            while ((int) num1_copy % 10 == (int) num2_copy % 10 && (int) num1_copy != 0 && (int) num2_copy != 0){
                flag = (int) num1_copy % 10;
                num1_copy = num1_copy * 10 - ((int) num1_copy % 10) * 10;
                num2_copy = num2_copy * 10 - ((int) num2_copy % 10) * 10;
            }
            System.out.println(num1+" "+num2+" "+num1_copy+" "+num2_copy);
            if ((int) num1_copy == 0 ){
                if (num2_copy % 10 > flag){
                    return -1;
                }else {
                    return 1;
                }
            } else if (num2_copy == (double) 0){
                if (num1_copy % 10 > flag){
                    return 1;
                } else {
                    return -1;
                }
            }else if (num1_copy % 10 > num2_copy % 10){
                return 1;
            } else {
                return -1;
            }
        }
        
        
    }
}

2、用另一种方法排序:

直接用String存储数字,两个数字(str1,str2)的大小:

s1 = str1 + str2;

s2 = str2 + str2;

s1.compareTo(s2);

这样比较。

public class Solution {
    public String largestNumber(int[] nums) {
        String[] strs = new String[nums.length];
        for (int i = 0; i < nums.length; i++){
            strs[i] = String.valueOf(nums[i]);
        }
        Comparator<String> comp = new Comparator<String>(){
            public int compare(String str1, String str2){
                String s1 = str1 + str2;
                String s2 = str2 + str1;
                return s2.compareTo(s1);
            }
        };
        Arrays.sort(strs, comp);
        if (strs[0].charAt(0) == '0'){
            return "0";
        }
        StringBuffer sb = new StringBuffer();
        for (String str : strs){
            sb.append(str);
        }
        return sb.toString();
    }
}