Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101" 这个题我们就把a,b补全, 使得他们length相等, 这样, 只需要判断最后一个edge case, 看看是不是需要多加一位.
class Solution:
def addBinary(self, a, b):
if len(b) < len(a):
a, b = b, a
m, n, ans, pre = len(a), len(b), "", 0
a = ''*(n-m) + a
for i in range(n)[::-1]: # note 从n-1往前走
pre, rem = divmod(int(a[i]) + int(b[i] + pre, 2))
ans += str(rem)
return ans[::-1] if not pre else '' + ans[::-1]

[LeetCode] 67. Add Binary_Easy tag: String的更多相关文章

  1. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  2. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  3. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  4. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  5. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  7. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  8. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  9. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

随机推荐

  1. Sencha Cmd 5.0.1.231 是坑爹货

    Sencha Cmd 5.0.1.231相比之前的版本有了很大的变动,存在很多坑爹之处,个人建议不要升级到这个版本,如果已经升级了的就卸载了还原到以前的版本吧. 历史版本下载地址:http://cdn ...

  2. sencha touch 在线实战培训 第一期 第八节 (完结)

    2014.1.15晚上8点开的课 这是本期课程的最后一课,下期课程预计在春节后继续. 如果你有什么意见和建议可以将他们发送到邮箱:534502520@qq.com 本期培训一共八节,前三堂免费,后面的 ...

  3. 基于spring-cloud的微服务(1) 服务注册中心eureka

    eureka是Netflix提供的服务注册中心组建,springcloud将其做了封装,作为自己的微服务架构中的一个注册中心组建 下面的例子在IDEA中启动一个eureka的实例,然后提供一个prov ...

  4. Cocoa Touch框架

    iOS – Cocoa Touch简介: iOS 应用程序的基础 Cocoa Touch 框架重用了许多 Mac 系统的成熟模式,但是它更加专注于触摸的接口和优化.UIKit 为开发者提供了在 iOS ...

  5. Unity3D笔记 英保通二

    一.访问另一个物体 1.代码中定义一个public的物体 例如:var target:Transform; 在面板上直接拖拽一个物体赋值给target 2.通过GameObject.Find(&quo ...

  6. Thinkphp 验证码点击刷新解决办法

    HTML代码如下: <span> <input type="text" name="code" placeholder="验证码&q ...

  7. backBone.js之Model篇 (1) 简单实例

    “Model是js应用的核心,包括基础的数据以及围绕着这些数据的逻辑:数据转换.验证.属性计算和访问控制”. 一.初始化方法 我们先来看一个demo,initialize,这是一个初始化方法,但是写这 ...

  8. Solve Error: 'has incomplete type', foward declaration of 'class x'

    在C++的OOB编程中,有时候我们会遇到这样的错误Error: 'has incomplete type',forward declaration of 'class x',那么是什么原因引起的这个问 ...

  9. 搭建本地DNS解析

    一.安装Dnsmasq yum install -y dnsmasq 二.安装dig yum install dnsmasq -y 三.编辑配置文件 vim /etc/dnsmasq.conf res ...

  10. MPD大会北京上海两站圆满落幕

    MPD大会北京上海两站圆满落幕 由麦思博(MSUP)主办的亚太软件研发团队管理峰会(以下简称MPD大会)分别于6月15及6月22日在北京.上海成功举办.国内外知名软件.互联网行业项目领头人及业内从业人 ...