### 描述

Given two binary strings a and b, return their sum as a binary string.

Example 1:

``````Input: a = "11", b = "1"
Output: "100"

Example 2:

``````Input: a = "1010", b = "1011"
Output: "10101"

Note:

``````1 <= a.length, b.length <= 10^4
a and b consist only of '0' or '1' characters.
Each string does not contain leading zeros except for the zero itself.

### 解析

• 初始化 M 和 N 得到 a 和 b 两个字符串的长度，保证 a 必须是较大的那个字符串
• 然后从它们的最后一位开始相加，满二往前进一
• 一直到遍历完 b 的所有字符，此时可能 a 还有需要遍历的字符，继续对 a 剩下的字符进行上面的相加操作
• 遍历结束所得到的字符串就是答案

### 解答

``````class Solution(object):
"""
:type a: str
:type b: str
:rtype: str
"""
M, N = len(a), len(b)
if M<N:
a,b = b,a
M,N = N,M
i,j = M-1,N-1
tmp = 0
result = ''
while i>-1 and j>-1:
c = int(a[i])+int(b[j])
if c + tmp > 1:
result = str(c+tmp-2) + result
tmp = 1
else:
result = str(c+tmp)+result
tmp = 0
i-=1
j-=1
while i>-1:
c = int(a[i]) + tmp
if c > 1:
result = str(c - 2) + result
tmp = 1
else:
result = str(c) + result
tmp = 0
i -= 1
if tmp>0:
result = '1' + result
return result

### 运行结果

``````Runtime: 45 ms, faster than 10.19% of Python online submissions for Add Binary.
Memory Usage: 13.5 MB, less than 50.08% of Python online submissions for Add Binary.

### 解析

• 将 a 和 b 转换为列表 a 和 b ，初始化进位变量 tmp 为 0 ，结果 result 为空字符串

• 当 a 不为空或 b 不为空或 tmp 不为空的时候：

（1）当 a 不为空 tmp += int(a.pop())

（2）当 b 不为空 tmp += int(b.pop())

（3）然后将 tmp%2 得到的该位置数字拼接到 result 前面

（4）tmp 需要整除 2

• 循环结束得到的 result 即为答案。

### 解答

``````class Solution(object):
"""
:type a: str
:type b: str
:rtype: str
"""
a = list(a)
b = list(b)
result = ''
tmp = 0
while a or b or tmp:
if a:
tmp += int(a.pop())
if b:
tmp += int(b.pop())
result = str(tmp%2) + result
tmp //= 2
return result

### 运行结果

``````Runtime: 29 ms, faster than 34.94% of Python online submissions for Add Binary.
Memory Usage: 13.6 MB, less than 50.08% of Python online submissions for Add Binary.

### 解答

``````class Solution(object):
"""
:type a: str
:type b: str
:rtype: str
"""
return  bin(int(a,2)+int(b,2))[2:]

### 运行结果

``````Runtime: 26 ms, faster than 48.42% of Python online submissions for Add Binary.
Memory Usage: 13.6 MB, less than 22.38% of Python online submissions for Add Binary.