Python solution (no map and no logical shift workaround)


  • 0
    L

    This problem is pretty annoying in python because python only has arithmetic shift and no logical shift. We can work around this by converting the full 32 bits to hex and stripping leading 0s if we have to.

        def toHex(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num == 0 : return '0'
            result = []
            for i in range(8) :
                print num
                val = num & 15
                result.append(str(val) if val < 10 else chr(val - 10 + ord('a')))
                num >>= 4 
            while result[-1] == '0' : result.pop()
            return ''.join(result[::-1])
    
    

Log in to reply
 

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