2165. Smallest Value of the Rearranged Number

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2165

class Solution:
    def smallestNumber(self, num: int) -> int:
        if num == 0:
            return 0
        cnt = [0] * 10
        neg = num < 0
        num = abs(num)
        while num:
            num, v = divmod(num, 10)
            cnt[v] += 1
        res = ""
        if neg:
            for i in range(9, -1, -1):
                if cnt[i]:
                    res += str(i) * cnt[i]
            return -int(res)
        if cnt[0]:
            for i in range(1, 10):
                if cnt[i]:
                    res += str(i)
                    cnt[i] -= 1
                    break
        for i in range(10):
            if cnt[i]:
                res += str(i) * cnt[i]
        return int(res)