2165.cpp (802B)
1 class Solution { 2 public: 3 long long smallestNumber(long long num) { 4 static uint8_t digits[16]; 5 bool neg = false; 6 uint8_t cnt = 0; 7 8 if (num < 0) { 9 num = -num; 10 neg = true; 11 } 12 13 do { 14 digits[cnt++] = num % 10; 15 } while ((num /= 10) > 0); 16 17 if (neg) 18 sort(begin(digits), begin(digits) + cnt, greater()); 19 else { 20 sort(begin(digits), begin(digits) + cnt); 21 for (int i = 0; i < cnt; i++) { 22 if (digits[i] == 0) continue; 23 swap(digits[0], digits[i]); 24 break; 25 } 26 } 27 28 num = 0; 29 for (int i = 0; i < cnt; i++) 30 num = (num * 10) + digits[i]; 31 return !neg ? num : -num; 32 } 33 };