#1017

Convert to Base -2

specialist · 730 · lc medium +31 · verified · 61.8% accepted · 572 likes · top 62%

Description

Given an integer n, convert it to base -2 and return the result as a binary string.

The returned string must have no leading zeros unless the value is "0".

Example 1:

Input: n = 2
Output: "110"
Explantion: (-2)2 + (-2)1 = 2

Example 2:

Input: n = 3
Output: "111"
Explantion: (-2)2 + (-2)1 + (-2)0 = 3

Example 3:

Input: n = 4
Output: "100"
Explantion: (-2)2 = 4

Code

1
2
3