straightforward python implementation


  • 0
    X
    class Solution(object):
        def toHex(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num != 0:
                if num>0:
                    num = num
                elif num<0:
                    num += 2**32
                s = []
                ss = []
                while num>0:
                    b = num%16
                    if b == 10:
                        s.append('a')
                    elif b == 11:
                        s.append('b')
                    elif b == 12:
                        s.append('c')
                    elif b == 13:
                        s.append('d')
                    elif b == 14:
                        s.append('e')
                    elif b == 15:
                        s.append('f')
                    else:
                        s.append(str(b))
                    num = num//16
                l = len(s)
                for i in range(l):
                    ss.append(s[l-1-i])
                return ''.join(ss)
            else:
                return '0'
    

  • 0
    I

    why do you do num = num + 2**32? what is that doing exaactly?


Log in to reply
 

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