LeetCode_AddBinary

Add Binary

Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0.
(二进制字符串加法)

Example:



1. 十进制 & 二进制转换

1
2
3
4
5
6
7
8
9
10
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a = int(a, 2)
b = int(b, 2)
return "{:b}".format(a + b)