405. Convert a Number to Hexadecimal

405. Convert a Number to Hexadecimal

šŸ’”
這個锌ē›®ęœ‰äø€å€‹ēŸ„č­˜é»žéœ€č¦å…ˆč¤‡ēæ’ļ¼šć€ŒäŗŒč£œę•ø怍
class Solution:
    def toHex(self, num: int) -> str:
    
        if num == 0:
            return '0'
    
        if num < 0:
            num = (1 << 32) + num
        
        hex_digits = '0123456789abcdef'
        hex_num = ''
        
        while num > 0:
            digit = num % 16
            hex_digit = hex_digits[digit]
            hex_num = hex_digit + hex_num
            num //= 16
        
        return hex_num