python solution by XOR


  • 0
    Y
        map = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
        tbin = bin(0xffffffff & num)[2:] if num >= 0 else bin( 0xffffffff ^ (-1 * num) - 1)[2:]
        size = len(tbin) / 4 + 1 if len(tbin) % 4 != 0 else len(tbin) / 4
        zero = 4 - len(tbin) % 4 if len(tbin) % 4 else 0
        tbin = '0' * zero + tbin
        res = []
        for i in range(size):
            res.append(map[int(tbin[4 * i : 4 * (i + 1)],2)])
        return ''.join(res)

Log in to reply
 

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