when program excuted the second if and change P =0,it will excute the next if. I know it's wrong,but i curiously wonder how to hold variables(P) constant and don't impact other in the if statement.
"""
:type a: str
:type b: str
:rtype: str
"""
class Solution(object):
def addBinary(self, a, b):
c = ''
P = 0
for i,j in zip(range(len(a)-1, -1, -1),range(len(b)-1, -1, -1)):
if a[i] + b[j] == '11':
c = c + '0'
P = 1
if a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 1:
c = c + '0'
P = 0
if a[i] + b[j] == '10' or a[i] + b[j] == '01' and P == 0:
c = c + '1'
if a[i] + b[j] == '00' and P == 1:
c = c + '1'
P =0
if a[i] + b[j] == '00' and P == 0:
c = c + '0'
T = len(a) - len(b)
if T > 0:
c = c + a[:T]
else:
c = c + b[:T]
return c[::-1]
Aucun commentaire:
Enregistrer un commentaire