Java with explanation, Easy to Understand
The base 2 expansion of a number can be found by repeated division by 2, recording the remainders, and concatenating those remainders, starting with the last. T
The base `2` expansion of a number can be found by repeated division by `2`, recording the remainders, and concatenating those remainders, starting with the last. The difference for base `-2` is the remainders may be negative. In this case, we add the quotient by `1` and add the remainder by `2` to make the remainders be positive. \nHere is the explanation:\nLet `d` represents the quotient and `r` represents the remainder, then \n`n = d * (-2) + r = d * (-2) + 2 - 2 + r = (d + 1) * (-2) + (r  + 2)`.\n\n```\nclass Solution {\n    public String baseNeg2(int n) {\n        if (n == 0) return "0";\n        StringBuilder sb = new StringBuilder();\n        while (n != 0) {\n            int remainder = n % (-2);\n            n /= (-2);\n            if (remainder < 0) {\n                remainder -= -2;\n                n += 1;\n            }\n            sb.append(remainder);\n        }\n        return sb.reverse().toString();\n    }\n}\n```
[]