$ cat ./coding/405-convert-a-number-to-hexadecimal.md
[Coding]
405. Convert a Number to Hexadecimal
────────────────────────────────────────────────────────────
405. Convert a Number to Hexadecimal
💡
這個題目有一個知識點需要先複習:「二補數」
class Solution:
def toHex(self, num: int) -> str:
if num == 0:
return '0'
if num < 0:
num = (1 << 32) + num
hex_digits = '0123456789abcdef'
hex_num = ''
while num > 0:
digit = num % 16
hex_digit = hex_digits[digit]
hex_num = hex_digit + hex_num
num //= 16
return hex_num
--tags#Bit Manipulation
$ ls ./coding/ | grep -v 405-convert-a-number-to-hexadecimal