Posted on 2023-02-14 17:43
Uriel 阅读(41)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
简单二进制加法,字符串先翻转,相加之后再翻转输出
1 #67
2 #Runtime: 20 ms (Beats 72.49%)
3 #Memory: 13.6 MB (Beats 24.25%)
4
5 class Solution(object):
6 def addBinary(self, a, b):
7 """
8 :type a: str
9 :type b: str
10 :rtype: str
11 """
12 a = a[::-1]
13 b = b[::-1]
14 t = 0
15 s = []
16 for i in range(0, max(len(a), len(b))):
17 if i < len(a):
18 t += int(a[i])
19 if i < len(b):
20 t += int(b[i])
21 s.append(str(t % 2))
22 t = t // 2
23 while t:
24 s.append(str(t % 2))
25 t = t // 2
26 return ''.join(s)[::-1]