LeetCode – Largest Number (Java)

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.)

Analysis

This problem can be solve by simply sorting strings, not sorting integer. Define a comparator to compare strings by concat() right-to-left or left-to-right.

Java Solution

public String largestNumber(int[] nums) {
    String[] arr = new String[nums.length];
    for(int i=0; i<nums.length; i++){
        arr[i]=String.valueOf(nums[i]);
    }
 
    Arrays.sort(arr, new Comparator<String>(){
        public int compare(String a, String b){
            return (b+a).compareTo(a+b);
        }
    });
 
    StringBuilder sb = new StringBuilder();
    for(String s: arr){
        sb.append(s);
    }
 
    while(sb.charAt(0)=='0'&&sb.length()>1)
        sb.deleteCharAt(0);
 
    return sb.toString();
}
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Ankit Shah on 2016-2-3

    Using collections.sort of string

    public class LargestNumber {
    private static String largestNumber(int[] a) {
    final String sortString = "0123456789";
    StringBuffer result = new StringBuffer();
    List list = new ArrayList();
    for (int i = 0; i < a.length; i++) {
    String element = String.valueOf(a[i]);
    for (int j = 1; j <= element.length(); j++) {
    list.add(element.substring(j - 1, j));
    }
    }

    Collections.sort(list, new Comparator() {
    public int compare(String s1, String s2) {
    return sortString.indexOf(s2) - sortString.indexOf(s1);
    }
    });

    System.out.println(list);

    for (String element : list) {
    result = result.append(element);
    }
    return result.toString();
    }

    public static void main(String[] args) {
    int[] a = { 3, 30, 34, 5, 9 };
    System.out.println(largestNumber(a));
    }

    }

  2. Jangku on 2016-6-30

    your comparison doesn’t work : i.e., “45” , “4”
    should be “454” instead of “445”

  3. Ankit Shah on 2016-12-26

    not sure on what case we need the following snippet


    while(sb.charAt(0)=='0'&&sb.length()>1)
    sb.deleteCharAt(0);

  4. Rohit Nair on 2017-1-28

    If the elements in the input list is all 0s

  5. Anonymus on 2017-3-8

    You might have written (a+b).compareTo(b+a) instead of (b+a).compareTo(a+b). This returns an ascending order and hence smallest number is formed.

  6. Sujeet Kumar Mehta on 2017-4-2

    this works. rather than the proposed solution in the article

  7. Rathnakar on 2017-5-19

    Why we need comparator.Arrays.sort will work for string type array.

  8. kyosau on 2017-7-25

    @Test
    public void testCompare() {
    int[] nums = {45,44,54,4,5,11,10,12,1,2,0};
    System.out.println(largestNumber(nums));
    }

    public String largestNumber(int[] nums) {
    String[] arr = new String[nums.length];
    for (int i = 0; i < nums.length; i++) {
    arr[i] = String.valueOf(nums[i]);
    }

    Arrays.sort(arr, new Comparator() {
    public int compare(String a, String b) {
    if (a.length() > b.length()) {
    return -1;
    }else if(a.length() < b.length()){
    return 1;
    }else if(a.length() == b.length()) {
    return (b + a).compareTo(a + b);
    }
    return 0;

    }
    });

    System.out.println(arr[0]);

    return arr[0];
    }

Leave a comment

*