Base 7

November 21, 2022

math-and-geometry

Problem URL: Base 7

We will divide the number by 7 and keep track of the remainder. We will add the remainder to the result and multiply the result by 10. We will repeat this process until the number is 0.

class Solution:
    def convertToBase7(self, num: int) -> str:
        n, res = abs(num), ''
        while n:
            res = str(n%7) + res
            n //= 7
        return '-' * (num < 0) + res or "0"

Time complexity: O(log(n))
Space complexity: O(1)