Convert to base -2

January 4, 2023

math-and-geometry

Problem URL: Convert to base -2

We will use the following formula to convert a number n to base -2, where n is a non-negative integer- - Write a base 2 function - If the number is odd, add 1 to the result - Divide the number by 2

class Solution:
    def baseNeg2(self, n: int) -> str:
        if n == 0:
            return '0'
        res = ''
        while n:
            res = str(n & 1) + res
            n = -(n >> 1)
        return res

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