Convert a Number to Hexadecimal LeetCode solution in Python
1# Runtime: 33 ms (Top 88.95%) | Memory: 13.8 MB (Top 62.28%)2class Solution:3def toHex(self, num: int) -> str:4ret = [5"0",6"1",7"2",8"3",9"4",10"5",11"6",12"7",13"8",14"9",15"a",16"b",17"c",18"d",19"e",20"f",21]22ans = ""2324if num < 0:25num = pow(2, 32) + num2627if num == 0:28return "0"29while num > 0:30ans = ret[num % 16] + ans31num = num // 163233return ans