Skip to content

Commit 0d8fa25

Browse files
committed
feat: adds convert_to_hex exercise
1 parent ecac4df commit 0d8fa25

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

convert_to_hex.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def toHex(self, num: int) -> str:
3+
if num == 0:
4+
return "0"
5+
if num < 0:
6+
num = (1 << 32) + num
7+
8+
hex_digits = "0123456789abcdef"
9+
10+
hex_num = ""
11+
12+
while num > 0:
13+
digit = num % 16
14+
hex_digit = hex_digits[digit]
15+
hex_num = hex_digit + hex_num
16+
num //= 16
17+
18+
return hex_num

0 commit comments

Comments
 (0)