25 ms solution


  • 0
    X
        def toHex(self, num):
            """
            :type num: int
            :rtype: str
            """
            hex_str = '0123456789abcdef'
            result = ""
            if num == 0:
                return '0'
            counter = 0
            while num != 0 and counter < 8:
                remainder = num % 16
                result = hex_str[remainder] + result
                num = (num >> 4)
                counter += 1
            return result```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.