forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT32.java
More file actions
37 lines (35 loc) · 1.15 KB
/
T32.java
File metadata and controls
37 lines (35 loc) · 1.15 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
34
35
36
37
package web; /**
* @program LeetNiu
* @description: 把数组排成最小的数
* @author: mf
* @create: 2020/01/14 00:33
*/
import java.util.Arrays;
import java.util.Comparator;
/**
* 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
* 例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
*/
public class T32 {
public String PrintMinNumber(int [] numbers) {
if (numbers == null || numbers.length == 0) return "";
int len = numbers.length;
String[] str = new String[len];
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
str[i] = String.valueOf(numbers[i]);
}
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String c1 = o1 + o2;
String c2 = o2 + o1;
return c1.compareTo(c2);
}
});
for (int i = 0; i < len; i++) {
sb.append(str[i]);
}
return sb.toString();
}
}