博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 67. Add Binary_Easy tag: String
阅读量:4463 次
发布时间:2019-06-08

本文共 724 字,大约阅读时间需要 2 分钟。

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 = '0'*(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 '1' + ans[::-1]

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/9465961.html

你可能感兴趣的文章
亲测可用,解决端口被占用的指令!!
查看>>
MySQL--视图、触发器、事务、存储过程、内置函数、流程控制、索引
查看>>
Django--数据库查询操作
查看>>
自定义配置文件的使用
查看>>
js-20170609-运算符
查看>>
算法笔记_065:分治法求逆序对(Java)
查看>>
MSP430FLASH小结
查看>>
STM32 ADC转换时间
查看>>
结合实际业务场景聊一聊MVP模式的应用
查看>>
WinPE启动U盘的制作方法与软件下载(通用PE工具箱/老毛桃/大白菜WinPE)(转载)...
查看>>
行为型设计模式之5--中介者模式
查看>>
Android DevArt6:Android中IPC的六种方式
查看>>
oracle练习题
查看>>
PMP学习感想
查看>>
Zookeeper全解析——Paxos作为灵魂
查看>>
集合-强大的集合工具类:java.util.Collections中未包含的集合工具
查看>>
CSS清除浮动
查看>>
数据库基础-数据库常用命令总结
查看>>
java8 按对象属性值排序
查看>>
【转帖】国产x86处理器KX-6000发布
查看>>