python solution beats 95% sometimes


  • 0
    S
    class Solution(object):
        def toHex(self, num):
            if num > 0:
            	n = num
            elif num == 0:
            	return '0'
            else:
            	n = 4294967295+1+num
            hex_dict = {10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'}
            hex_list = []
            while n > 0:
            	hex_list.append(n%16)
            	n = n // 16
            for i in range(len(hex_list)):
            	if hex_list[i] > 9:
            		hex_list[i] = hex_dict[hex_list[i]]
            	else:
            		hex_list[i] = str(hex_list[i])
            return ''.join(hex_list[::-1])
    

    This solution is long, but easy to understand and it beats 95% sometimes.


Log in to reply
 

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