-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path67.add-binary.py
More file actions
42 lines (38 loc) · 961 Bytes
/
67.add-binary.py
File metadata and controls
42 lines (38 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Tag: Math, String, Bit Manipulation, Simulation
# Time: O(M+N)
# Space: O(1)
# Ref: -
# Note: -
# 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"
#
#
# Constraints:
#
# 1 <= a.length, b.length <= 104
# a and b consist only of '0' or '1' characters.
# Each string does not contain leading zeros except for the zero itself.
#
#
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = ''
carry = 0
i = len(a) - 1
j = len(b) - 1
while i >= 0 or j >= 0 or carry:
l = int(a[i]) if i >= 0 else 0
r = int(b[j]) if j >= 0 else 0
tmp = l + r + carry
digit = tmp % 2
carry = tmp // 2
res = str(digit) + res
i -= 1
j -= 1
return res