File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def addBinary (self , a , b ):
3+ """
4+ :type a: str
5+ :type b: str
6+ :rtype: str
7+ """
8+ la = len (a )
9+ lb = len (b )
10+ if la < lb :
11+ a ,b = b ,a
12+ b = b .zfill (max (la ,lb ))
13+ a = list (a )
14+ b = list (b )
15+ la = len (a )
16+ lb = len (b )
17+ for i in range (la ):
18+ a [i ] = int (a [i ])
19+ for i in range (lb ):
20+ b [i ] = int (b [i ])
21+ if la < lb :
22+ a ,b = b ,a
23+
24+
25+ for i in range (len (b ) - 1 , - 1 , - 1 ):
26+ a [i ] = (int (a [i ]) + int (b [i ]))
27+ for i in range (len (a ) - 1 , - 1 , - 1 ):
28+ benwei = a [i ] % 2
29+ jinwei = a [i ] / 2
30+ a [i ] = benwei
31+ if i > 0 :
32+ a [i - 1 ] += jinwei
33+ if 0 == i and 1 == jinwei :
34+ a .insert (0 ,1 )
35+ for i in range (len (a )):
36+ a [i ] = str (a [i ])
37+ #print a
38+ a = "" .join (a )
39+ return a
You can’t perform that action at this time.
0 commit comments