# 重排数字的最小值
import java.util.Arrays;
/**
* @author yagol
* @date 上午10:43
* @desc the description of this class
**/
class Solution6001 {
public static void main(String[] args) {
System.out.println(new Solution6001().smallestNumber(-1));
}
public long smallestNumber(long num) {
if (num == 0) {
return 0;
}
String str = String.valueOf(num);
if (num < 0) {
str = str.replace("-", "");
}
char[] data = str.toCharArray();
Arrays.sort(data);
String newstr = "";
String countzero = "";
for (int i = 0; i < data.length; i++) {
if (num < 0) {
newstr += data[data.length - 1 - i];
} else {
if (data[i] == '0') {
countzero += "0";
continue;
}
newstr += data[i];
}
}
if (num < 0) {
System.out.println(newstr);
return Long.parseLong("-" + newstr);
}
newstr = newstr.charAt(0) + countzero + newstr.substring(1);
return Long.parseLong(newstr);
}
}
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
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