-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlargestNumber.java
30 lines (24 loc) · 947 Bytes
/
largestNumber.java
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
class Solution {
public String largestNumber(int[] nums) {
if(nums == null || nums.length == 0) return "";
String [] numStr = new String[nums.length];
for(int i=0; i<nums.length; i++)
numStr[i] = nums[i]+"";
Arrays.sort(numStr, new Comparator<String>()
{
@Override
public int compare(String i, String j)
{
String s1 = i+j;
String s2 = j+i;
return s1.compareTo(s2);
}
}
);
if(numStr[nums.length-1].charAt(0) == '0') return "0";
String result = new String();
for(int i=0; i<numStr.length; i++)
result = numStr[i]+result;
return result;
}
}