-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2165.py
34 lines (29 loc) · 955 Bytes
/
m2165.py
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
class Solution:
def smallestNumber(self, num: int) -> int:
if num == 0 :
return num
isNeg = False
if num < 0 :
isNeg = True
num = -num
digitCount = [0] * 10
smallestDigit = 10
while num > 0 :
if num % 10 < smallestDigit and num % 10 != 0 :
smallestDigit = num % 10
digitCount[num % 10] += 1
num //= 10
if isNeg : # basically get largest val
output = 0
for i in range(9, -1, -1) :
for _ in range(digitCount[i]) :
output = 10 * output + i
return -1 * output
output = smallestDigit # since no leading zeros
digitCount[smallestDigit] -= 1
for i in range(10) :
while digitCount[i] :
output *= 10
output += i
digitCount[i] -= 1
return output