504. Base 7
class Solution:
def convertToBase7(self, num: int) -> str:
if num == 0:
return "0"
neg = num < 0
num = num * -1 if neg else num
res = ""
while num // 7 != 0 or num % 7 != 0:
res = res + str((num % 7))
num = num // 7
if neg:
res = res + "-"
res = res[::-1]
return str(res)