c++ easy solution with explanation
First, we know that every number in computer system is already represented in base 2, which means for every number it can be converted to {a_31 ... a_0 }. (Beca
First, we know that every number in computer system is already represented in base 2, which means for every number it can be converted to {a_31 ... a_0 }. (Because int is 32 bits long)\n\nThen for a_i, if a_i == 1 and i is an even number, we do nothing, because (-2)^2n = (2)^2n.\n\nHowever, if a_i == 1, and i is a odd number, we have to update it, because (-2)^(2n+1) != (2)^(2n+1).\nLuckily enough, we know that (2)^(2n+1) = (2)^(2n+2) - (2)^(2n+1) = (-2)^(2n+2) + (-2)^(2n+1).\n\nSo, if a_i == 1 and i is a odd number, we need represent {a_i = 1} as { a_i+1 = 1, a_i = 1}.\n\nHere is the codes.\n```\nclass Solution {\npublic:\n    string baseNeg2(int N) {\n        bitset<32> b(N);\n        int c = 0;\n        string ret = "";\n        for(int i = 0;i<32;i++){\n            int t = b[i] + c;\n            if(i&1){\n                if(t == 2){\n                    ret = "0"+ret;\n                    c = 1;\n                } else if(t == 1){\n                    ret = "1" + ret;\n                    c = 1;\n                } else{\n                    ret = "0" + ret;\n                    c = 0;\n                }\n            } else {\n                if(t == 2){\n                    ret = "0" + ret;\n                    c = 1;\n                } else {\n                    ret = to_string(t) + ret;\n                    c = 0;\n                }\n            }\n        }\n        if(c == 1){\n            ret = "1" + ret;\n        }\n        ret.erase(ret.begin(), std::find_if(ret.begin(), ret.end(), [](int ch) {return ch != \'0\';}));\n        return ret.empty()?"0":ret;\n    }\n};\n```
[]