Python Solution


  • 0
    P
    class Solution(object):
        def toHex(self, num):
            mask = 0xFFFFFFFF
            def helper(x):
                ans = ''
                dict = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
                for i in range(9)[::-1]:
                    n = x >> 4*i
                    if n or ans:
                        ans += dict.get(n, str(n))
                        x -= n * (16 ** i)
                return ans if ans else '0'
            return helper(num) if num >= 0 else helper(num & mask)
    

Log in to reply
 

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