Base 7

Problem Id: 504 Difficulty: Easy


Intuition

Solution


class Solution:
    def convertToBase7(self, num: int) -> str:
        if num == 0:
            return '0'
        neg = num < 0
        num = abs(num)
        base7 = []
        while num:
            base7.append(num % 7)
            num = num // 7
        ans = ''.join([str(i) for i in base7][::-1])
        if neg:
            ans = '-' + ans
        return ans