leetcode/0504_base-7/python3/solution.py

12 lines
284 B
Python
Raw Normal View History

2022-04-11 07:29:55 +00:00
class Solution:
def convertToBase7(self, num: int) -> str:
sign = '-' if num < 0 else ''
num = abs(num)
b7 = ''
while num > 0:
b7 = str(num % 7) + b7
num //= 7
return '0' if b7 == '' else sign + b7