File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+
3+ class Solution :
4+ # @param {string} a a number
5+ # @param {string} b a number
6+ # @return {string} the result
7+ def addBinary (self , a , b ):
8+ # Write your code here
9+ flag = 0
10+ ret = ''
11+ a_i , b_i = len (a ) - 1 , len (b ) - 1
12+ while (a_i >= 0 ) and (b_i >= 0 ):
13+ va , vb = int (a [a_i ]), int (b [b_i ])
14+ ret += str ((va + vb + flag ) % 2 )
15+ flag = (va + vb + flag ) / 2
16+ a_i -= 1
17+ b_i -= 1
18+ c_i = - 1
19+ if (a_i >= 0 ) or (b_i >= 0 ):
20+ c = a if a_i >= 0 else b
21+ c_i = a_i if a_i >= 0 else b_i
22+ while c_i >= 0 :
23+ ret += str ((int (c [c_i ]) + flag ) % 2 )
24+ flag = (int (c [c_i ]) + flag ) / 2
25+ c_i -= 1
26+ if flag :
27+ ret += str (flag )
28+ return ret [::- 1 ] # Python没有提供直接反转字符串的函数,用切片凑合一下吧。
You can’t perform that action at this time.
0 commit comments