Convert to Base -2 LeetCode solution in C++

1
class Solution {
2
public:
3
string baseNeg2(int n) {
4
if (n == 0) return "0";
5
string ans = "";
6
while (n != 0) {
7
int rem = n % -2;
8
if (rem < 0) {
9
rem += 2;
10
n--;
11
}
12
ans = to_string(rem) + ans;
13
n = n / -2;
14
}
15
return ans;
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0