Integer to Binary Representation
Problem statement
Convert the signed 32-bit integer n into a binary string using only 0 and 1.
For this exercise, use the conventional 32-bit two's-complement representation for negative inputs. For nonnegative inputs omit leading zeros, except that zero returns "0". Do not include a sign or a binary prefix.
Function
toBinary(n: int) → StringExamples
Example 1
n = 10return = 1010Ten equals 8+2, so its binary representation is 1010.
Example 2
n = 0return = 0Zero returns the single character 0.
Constraints
-2147483648 <= n <= 2147483647- Negative inputs use exactly 32 two's-complement bits.