Problem:
Given two binary strings, return their sum (also a binary string).For example, a = "11" b = "1", return "100".
Similar to Decimal Addition, using a carry to store the current digit and carry to next digit.
For dth digit
carry = carry + a[d] + b[d]//if a or b's length < d, just add zero
c[d] = carry%base
carry = carry/base
End
If using a string builder, the result is in reverse order.
Note finally the carry may be non zero.
No comments:
Post a Comment