leetcode LargestNumber
z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Comparator;

import static java.util.Arrays.sort;

public class LargestNumber {
/*
Given a list of non negative integers, arrange them such that they form the largest number.

Example 1:

Input: [10,2]
Output: "210"
Example 2:

Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.*/
public static String largestNumber(int[] nums) {
int n = nums.length;
String[] s = new String[n];
for (int i=0; i<n; i++){
s[i] = String.valueOf(nums[i]);
}
Comparator<String> c = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o2+o1).compareTo(o1+o2);
}
};
sort(s, c);
String result = String.join("", s);
return result;
}

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