-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2165.cpp
More file actions
33 lines (29 loc) · 802 Bytes
/
Copy path2165.cpp
File metadata and controls
33 lines (29 loc) · 802 Bytes
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
class Solution {
public:
long long smallestNumber(long long num) {
static uint8_t digits[16];
bool neg = false;
uint8_t cnt = 0;
if (num < 0) {
num = -num;
neg = true;
}
do {
digits[cnt++] = num % 10;
} while ((num /= 10) > 0);
if (neg)
sort(begin(digits), begin(digits) + cnt, greater());
else {
sort(begin(digits), begin(digits) + cnt);
for (int i = 0; i < cnt; i++) {
if (digits[i] == 0) continue;
swap(digits[0], digits[i]);
break;
}
}
num = 0;
for (int i = 0; i < cnt; i++)
num = (num * 10) + digits[i];
return !neg ? num : -num;
}
};