1
class Solution {
2
public long smallestNumber(long num) {
3
if (num == 0) {
4
return 0;
5
}
6
boolean isNegative = num < 0;
7
num = num < 0 ? num * -1 : num;
8

9
char[] c = String.valueOf(num).toCharArray();
10
Arrays.sort(c);
11
String str;
12
if (!isNegative) {
13
int non = 0;
14
// if not negative we need to find out the first non-leading zero then swap with first zero
15
for (; non < c.length; non++) {
16
if (c[non] != '0') {
17
break;
18
}
19
}
20
char temp = c[non];
21
c[non] = c[0];
22
c[0] = temp;
23
str = new String(c);
24
} else {
25
str = new String(c);
26
StringBuilder sb = new StringBuilder(str);
27
str = "-".concat(sb.reverse().toString());
28
}
29
return Long.valueOf(str);
30
}
31
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0