forked from shenzhu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179. Largest Number.java
More file actions
33 lines (28 loc) · 1.02 KB
/
179. Largest Number.java
File metadata and controls
33 lines (28 loc) · 1.02 KB
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
public class Solution {
class stringComp implements Comparator<String>{
@Override
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
}
public String largestNumber(int[] nums) {
if(nums == null || nums.length == 0) return "";
// Convert int to String
String[] numsString = new String[nums.length];
for(int i = 0; i < nums.length; i++){
numsString[i] = String.valueOf(nums[i]);
}
// Sort String based on self-defined rull
Arrays.sort(numsString, new stringComp());
// An extreme corner case by lc, say you have only a bunch of 0 in your int array
if(numsString[0].charAt(0) == '0') return "0";
// Construct answer
StringBuilder sb = new StringBuilder();
for(String s : numsString){
sb.append(s);
}
return sb.toString();
}
}