Python clean solution based on math
Here is a straightforward math deduction (still a little tricky): The idea is that, we can repeatedly minus the last digit from current N, and then divided by -
Here is a straightforward math deduction (still a little tricky): The idea is that, we can repeatedly minus the last digit from current N, and then divided by -2 to form a loop situation. \n\nFor example, 2 = 1*(-2)^2 + 1 * (-2)^1 + 0 * (-2) ^0, so first get mod by -2. In python the mod returns the same sign as divisor, therefore, the remainder can be either -1 or 0. When it is -1, actually the real digit should be 1, because 1%(-2) = -1. So we change r to 1. After this step we deduct remainder from N: 1*(-2)^2 + 1 * (-2)^1 = 2 - r = 2(in this case r = 0). \n\nDivided by -2, we get 1*(-2)^1 + 1 * (-2)^0 = -1. At this point, we can start repeating. That is, mod by -2 get -1, meaning the last digit r = 1, then we get 1*(-2)^1 = -1 - r * (-2)^0 = -2; -2 divided by -2, we got 1. Repeat agin, r = 1, and N = 1 - r = 0, end. Got 110 as result.\n\n```\nclass Solution:\n    def baseNeg2(self, N: int) -> str:\n        res = []\n        while N != 0:\n            r = N % (-2)\n            if r == -1:\n                r = 1\n            N = (N - r) // (-2)\n\n            #print(r, N)\n            res.append(str(r))\n            \n        return \'\'.join(res[::-1]) or "0"\n
[]