67. Add Binary [Easy] 加法

67. Add Binary

67. Add Binary
class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if len(a) < len(b):
            return self.addBinary(b, a)
        b = '0' * (len(a) - len(b)) + b
        temp = 0
        res = ''
        for i in range(len(a)-1, -1, -1):
            ans = int(a[i]) + int(b[i]) + temp
            if ans == 0:
                res = '0' + res
                temp = 0
            elif ans == 1:
                res = '1' + res
                temp = 0
            elif ans == 2:
                res = '0' + res
                temp = 1
            else:
                res = '1' + res
                temp = 1
        if temp == 1:
            res = '1' + res
        return res
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,727评论 0 3
  • title: Add Binarytags:- add-binary- No.67- simple- boolea...
    yangminz阅读 155评论 0 0
  • 分类:Math/String 时间复杂度: O(n) 67. Add Binary Given two binar...
    野生小熊猫阅读 242评论 0 0
  • 67. Add Binary 题目:https://leetcode.com/problems/add-binar...
    oo上海阅读 254评论 0 0
  • 深入浅出 异曲同工 出生入死 古往今来 天南地北 古今中外 柳暗花明 转危为安 醉生梦死 无独有偶 水落石出 九死...
    弯月长虹阅读 211评论 0 0