GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/405_Convert_a_Number_to_Hexadecimal.py at master · qiyuangong/leetcode Expand file tree/ Copy path405_Convert_a_Number_to_Hexadecimal.pyMore file actions 31 lines (28 loc) · 848 Bytes / Copy path405_Convert_a_Number_to_Hexadecimal.pyFile metadata and controls31 lines (28 loc) · 848 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class Solution(object): def toHex(self, num): """ :type num: int :rtype: str """ if num == 0: return '0' # letter map mp = '0123456789abcdef' ans = '' for _ in range(8): # get last 4 digits # num & 1111b n = num & 15 # hex letter for current 1111 c = mp[n] ans = c + ans # num = num / 16 num = num >> 4 #strip leading zeroes return ans.lstrip('0') # def toHex(self, num): # def tohex(val, nbits): # return hex((val + (1 << nbits)) % (1 << nbits)) # return tohex(num, 32)[2:] # def toHex(self, num, h=''): # return (not num or h[7:]) and h or self.toHex(num / 16, '0123456789abcdef'[num % 16] + h) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |