python solution,easy to understand


  • 0
    W
    import numpy as np
    class Solution(object):
        def toHex(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num==0 :
                return '0'
            n=np.uint32(num)
            tmp=''
            while n:
                mod=n%16
                tmp+=chr(mod+ord('0')) if mod<10 else chr(mod-10+ord('a'))
                n/=16
            return tmp[::-1]
    

Log in to reply
 

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