179.largest-number


179. 最大数

给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。

示例 1:

输入: [10,2]
输出: 210

示例 2:

输入: [3,30,34,5,9]
输出: 9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。

解法一

class Solution {
    private class LargestNumberComparator implements Comparator<String> {
      @Override
      public int compare(String o1, String o2) {
        return (o2+o1).compareTo(o1+o2);
      }
    }
    public String largestNumber(int[] nums) {
      String[] str = new String[nums.length];
      for(int i = 0; i < nums.length; i++) {
        str[i] = String.valueOf(nums[i]);
      }
      Arrays.sort(str, new LargestNumberComparator());
      if (str[0].equals("0")) {
        return "0";
      }
      String result = new String();
      for(int i = 0; i < str.length; i++) {
        result += str[i];
      }
      return result;
    }
}

小优化

class Solution {

    private class largestNumberComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return (o2+o1).compareTo(o1+o2);
        }
    }

    public String largestNumber(int[] nums) {
        String[] str = new String[nums.length];
        for(int i = 0; i < nums.length; i++) {
            str[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(str, new largestNumberComparator());

        if (str[0].equals("0")) return "0";

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < str.length; i++) {
            sb.append(str[i]);
        }
        return sb.toString();
    }
}