Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Full Adder Logic:
- S = Cin ^ X ^ Y
- Cout = Cin&X + X&Y + Y&Cin
- Final Result = Cout.S(final)
- Initial c = 0, s = 0
- '''
- class Solution:
- def addBinary(self, a: str, b: str) -> str:
- cin = 0
- res = []
- m, n = len(a), len(b)
- for i in range(max(m, n)):
- s = cin
- cout = 0
- if i < m:
- x = int(a[-i-1])
- s ^= x
- cout |= cin & x
- if i < n:
- y = int(b[-i-1])
- s ^= y
- cout |= cin & y
- if i < m and i < n:
- cout |= x & y
- res.append(str(s))
- cin = cout
- if cin:
- res.append(str(cin))
- return ''.join(res[::-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement