Leetcode Problem 67. Add Binary

67. Add Binary

Leetcode Solutions

Bit-by-Bit Computation

  1. Initialize an empty string result to store the binary sum.
  2. Initialize variables i and j to point to the last characters of strings a and b respectively.
  3. Initialize carry to 0 to keep track of the carry from the addition of two bits.
  4. While i or j is greater than or equal to 0 or carry is not 0: a. Compute the sum of carry and the last bits of a and b if i or j is valid. b. Update carry to be the sum divided by 2 (to represent the carry in binary addition). c. Append the sum modulo 2 to the result string (to represent the bit value). d. Decrement i and j.
  5. Reverse the result string to get the final binary sum.
  6. Return the result string.
UML Thumbnail

Built-in Function Conversion

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...