Convert to Base -2 LeetCode solution in Python

1
class Solution:
2
def baseNeg2(self, n: int) -> str:
3
ans = ""
4
while n != 0:
5
if n % -2 != 0:
6
ans = "1" + ans
7
n = (n - 1) // -2
8
else:
9
ans = "0" + ans
10
n = n // -2
11
return ans if ans != "" else "0"

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0