forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAC_sort_nlogn.java
45 lines (37 loc) · 1.06 KB
/
AC_sort_nlogn.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_sort_nlogn.java
* Create Date: 2015-02-26 10:40:55
* Descripton:
*/
import java.util.Comparator;
import java.util.Scanner;
public class Solution {
public String largestNumber(int[] num) {
String[] str = new String[num.length];
for (int i = 0; i < num.length; ++i) {
str[i] = num[i] + "";
}
Comparator<String> comp = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o2 + o1).compareTo(o1 + o2); // reverse
}
};
Arrays.sort(str, comp);
if (str[0].equals("0")) {
return "0";
}
String ans = new String();
for (String s : str)
ans += s;
return ans;
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {0, 0, 0};
System.out.println(s.largestNumber(input));
}
}