Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

 

1 class Solution(object):
2     def addBinary(self, a, b):
3         """
4         :type a: str
5         :type b: str
6         :rtype: str
7         """
8         return bin(int(a,2)+int(b,2))[2:]

bin():将整数x转换为二进制字符串,前面会带'0b';

int(a,2),a是二进制,将其转换为十进制。

 

1 class Solution:
2     def addBinary(self, a, b):
3         return bin(eval('0b' + a) + eval('0b' + b))[2:]

eval()可以将字符串当成表达式来求值

posted on 2017-03-14 18:55  Ci_pea  阅读(118)  评论(0编辑  收藏  举报