bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1017. Convert to Base -2

1017. Convert to Base -2

Leetcode Solutions

Iterative Conversion to Base -

  1. Initialize an empty string result to store the binary representation.
  2. If n is 0, return '0' as the binary representation.
  3. While n is not equal to 0: a. Calculate the remainder of n divided by -2. b. If the remainder is negative, add 2 to the remainder and increment n by 1. c. Divide n by -2. d. Append the remainder to the result string.
  4. Reverse the result string to get the correct binary representation.
  5. Return the result string.
UML Thumbnail

Recursive Conversion to Base -

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...