class Solution:
def addBinary(self, a: str, b: str) -> str:
result = ''
carry = 0
for i in range(max(len(a), len(b))):
a_bit = a[-(i+1)] if i < len(a) else 0
b_bit = b[-(i+1)] if i < len(b) else 0
sum_bit = int(a_bit) + int(b_bit) + carry
result = str(sum_bit % 2) + result
carry = sum_bit // 2
if carry:
result = '1' + result
return result
leetcode-二进制求和
最新推荐文章于 2024-08-15 05:02:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)