File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Solution_61_70 (object ):
4
+ def addBinary (self , a , b ):
5
+ """
6
+ 67
7
+ :type a: str
8
+ :type b: str
9
+ :rtype: str
10
+ """
11
+ if len (a ) > len (b ):
12
+ max_len = len (a )
13
+ b = '0' * (len (a ) - len (b )) + b
14
+ else :
15
+ max_len = len (b )
16
+ a = '0' * (len (b ) - len (a )) + a
17
+
18
+ result = []
19
+ carry = 0
20
+ for ch_a , ch_b in zip (a [::- 1 ], b [::- 1 ]):
21
+ bit = int (ch_a ) + int (ch_b ) + carry
22
+ if bit > 1 :
23
+ result .append (str (bit - 2 ))
24
+ carry = 1
25
+ else :
26
+ result .append (str (bit ))
27
+ carry = 0
28
+
29
+ if carry == 1 :
30
+ result .append ('1' )
31
+
32
+ return '' .join (result [::- 1 ])
Original file line number Diff line number Diff line change
1
+ # -*- coding: utf-8 -*-
2
+ from sln_1_100 .solution_61_70 import Solution_61_70
3
+ from unittest import TestCase
4
+ class TestSolution_61_70 (TestCase ):
5
+ def setUp (self ):
6
+ self .sln = Solution_61_70 ()
7
+
8
+ def test_addBinary (self ):
9
+ ret = self .sln .addBinary ('11' ,'1' )
10
+ print (ret )
You can’t perform that action at this time.
0 commit comments