Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 810 Bytes

067._add_binary.md

File metadata and controls

31 lines (24 loc) · 810 Bytes

###67. Add Binary

题目: https://leetcode.com/problems/add-binary/

难度 : Easy

几种case:

  • a or b 为空,最简单
  • 唯一的问题是如果有进位的处理,进位的处理就是先让其中的一个数和‘1’addBinary处理 ,然后再用addBinary
class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if (a == '' or b == ''):
            return a + b
        elif a[-1] == '0' and b[-1] == '0':
            return self.addBinary(a[:-1], b[:-1])  +  '0'
        elif a[-1] == '1' and b[-1] == '1':
            return self.addBinary(a[:-1], self.addBinary(b[:-1],'1')) + '0'
        else:
            return self.addBinary(a[:-1], b[:-1]) + '1'