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. path 与classpath针对JAVA来说

    Path 路径,是java编译时需要调用的程序(如java,javac等)所在的地方CLASSPATH 类的路径,在编译运行java程序时,如果有调用到其他类的时候,在classpath中寻找需要的类 ...

  2. 【咸鱼教程】JsZip压缩与解压教程

    引擎版本3.0.6 教程目录一 为什么要用jszip二 如何使用jszip    2.1 下载jszip库    2.2 导入jszip库    2.3 加载和解压zip代码三 Demo源码下载 一 ...

  3. HTML5 Canvas 画虚线组件

    前段时间由于项目需要,用到了HTML5 Canvas画图,但是没有画虚线的方法,自己写了一个HTML5 画虚线的组件. dashedLine.js if (window.CanvasRendering ...

  4. Javascript-Object-Definition

    /* 定义对象的方法:构造函数,函数字面量法,工厂模式,构造函数模式 */ /**************************************/ /** **/ /** 1.原生构造函数法 ...

  5. layer.load()加载层如何加入文字描述

    https://fly.layui.com/jie/3586/ https://www.layui.com/doc/modules/layer.html#layer.load //loading层va ...

  6. vue--使用定时器的问题

    https://blog.csdn.net/ywl570717586/article/details/79963162

  7. vue--父组件主动获取子组件的方法

    父组件主动获取子组件的方法和属性 第一步:调用自组件的时候,给自组建定义一个Header <v-header ref='headerInfo'></v-header> 第二步: ...

  8. 国外很有多优秀的HTML5前端开发框架

    国外很有多优秀的HTML5前端开发框架 相信大家都耳熟能详:JQuery Mobile,Twitter Bootstrap, Schena Touch,  BackBone等等. 同样,也存在很多国内 ...

  9. Css 中的 block,inline和inline-block概念和区别

    1.block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通常被现 ...

  10. ABP之应用服务(2)

    在上一篇的笔记中,已经大致对Application层的使用作了简要的使用说明,感觉还是有些东西需要研究一下,所以承接上文,对AutoMapper这个方便的东西,稍微研究一下. 一.初识AutoMapp ...