-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path405.py
33 lines (32 loc) · 1.09 KB
/
405.py
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
32
33
__________________________________________________________________________________________________
sample 16 ms submission
class Solution:
def toHex(self, num: int) -> str:
if num == 0: return '0'
x = num.to_bytes(4, byteorder='big', signed=True).hex()
count = 0
for i in x:
if i == '0':
count += 1
else:
break
return x[count:]
__________________________________________________________________________________________________
sample 13152 kb submission
class Solution:
def toHex(self, num: int) -> str:
ans = ''
if num < 0:
num = 0x100000000 + num
while num > 0:
digit = num % 16
if digit < 10:
ans = chr(ord('0') + digit) + ans
else:
ans = chr(ord('a') + digit - 10) + ans
num //= 16
if len(ans) == 0:
return '0'
else:
return ans
__________________________________________________________________________________________________